2012-08-24 22 views
13

मैं एक ऑटोकंपलेटटेक्स्टव्यू लागू कर रहा हूं और मुझे अपने सभी संपर्कों का नाम और ई-मेल चाहिए। मुझे यह स्निपेट मिला कि मैं असीमित रूप से चल रहा हूं लेकिन यह बहुत धीमा है।संपर्क सूची से नाम और ईमेल प्राप्त करना बहुत धीमा है

ContentResolver cr = getContentResolver(); 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

if (cur.getCount() > 0) {    
    while (cur.moveToNext()) {     
     String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));     
     String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

     Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null); 

      while (emailCur.moveToNext()) { 

       String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
        autoCompleteAdapter.add(name + " - " + email); 
      } 

      emailCur.close(); 
     } 
    } 
} 

मैं एक प्रकार की आंतरिक क्वेरी कर रहा हूं और मुझे लगता है कि यह समस्या है। क्या इसे ट्यून करने और इसे तेज बनाने का कोई तरीका है?

+0

क्या आप इसे काम करने में कामयाब रहे? –

+0

बाहरी 'if (cur.getCount()> 0) 'अनावश्यक है और हटाया जा सकता है। – mdup

उत्तर

46
private static final String[] PROJECTION = new String[] { 
    ContactsContract.CommonDataKinds.Email.CONTACT_ID, 
    ContactsContract.Contacts.DISPLAY_NAME, 
    ContactsContract.CommonDataKinds.Email.DATA 
}; 

... 

ContentResolver cr = getContentResolver(); 
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, null, null, null); 
if (cursor != null) { 
    try { 
     final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID); 
     final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 
     final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); 
     long contactId; 
     String displayName, address; 
     while (cursor.moveToNext()) { 
      contactId = cursor.getLong(contactIdIndex); 
      displayName = cursor.getString(displayNameIndex); 
      address = cursor.getString(emailIndex); 
      ... 
     } 
    } finally { 
     cursor.close(); 
    } 
} 

कुछ नोट:

  • उपयोग सिर्फ ContactsContract.CommonDataKinds.Email.CONTENT_URI अपनी आवश्यक जानकारी प्राप्त करने के लिए, जानकारी क्या कॉलम आप
  • उपयोग प्रक्षेपण क्वेरी केवल उन स्तंभों तुम सच में की जरूरत है पाने के लिए कर सकते हैं के लिए ContactsContract.CommonDataKinds.Email देखते हैं, आप को बचाने के कुछ मेमोरी और क्वेरी प्रदर्शन बढ़ाएं
  • कॉलम इंडेक्स केवल एक बार प्राप्त करें, बस चक्र से पहले
+0

पूरी तरह से काम करता है। धन्यवाद –

+0

एक आकर्षण की तरह काम ..:) – ADT

+0

thanx alot, अद्भुत सुधार –

5

आप क्वेरी नहीं चाहिए सीधे ContactsContract.Contacts

मेक सिर्फ एक ईमेल डेटा प्रकार के साथ ContactsContract.CommonDataKinds पर क्वेरी।

ContactsContract.CommonDataKinds.Email कई अन्य इंटरफेस प्राप्त करता है जिनका उपयोग आप अपने प्रक्षेपण के लिए कर सकते हैं। (प्रलेखन से विरासत में मिली स्थिरांक देखें)

उदाहरण के लिए:

import android.provider.ContactsContract.CommonDataKinds.Email; 

[...] 

public static final String[] EMAILS_PROJECTION = new String[] { 
    Email._ID, 
    Email.DISPLAY_NAME_PRIMARY, 
    Email.ADDRESS 
}; 

Email.CONTENT_URI 

साथ प्रयोग किया जा करने के लिए आप (जैसे उपयोगकर्ता आईडी के रूप में, उपयोगकर्ता प्रदर्शन नाम जानकारी का एक बहुत कुछ प्राप्त कर सकते हैं ...) सीधे ईमेल डेटा प्रकार से।

संपादित करें:

मैं सिर्फ एहसास हुआ कि आप एक AutoCompleteTextView का निर्माण करने की कोशिश कर रहे हैं।

आप runQueryOnBackgroundThread विधि और अपने CursorAdapter की convertToString ओवरराइड और Email.CONTENT_FILTER_URI

मैं वास्तव में दृढ़ता से सुझाव का उपयोग ApiDemo नमूने पर एक नज़र लेने के लिए करना चाहिए।

विशेष रूप से AutoComplete4.java नमूना है कि आप HERE पा सकते हैं।

+1

धन्यवाद Timothée, आपकी व्याख्या बहुत अच्छी तरह से किया जाता है। बहुत बुरा मैं दोनों उत्तरों को सही के रूप में चिह्नित नहीं कर सकता। मुझे लगता है कि एक त्वरित स्निपेट की तलाश में अन्य उपयोगकर्ताओं के लिए biegleux का जवाब बहुत अच्छा है (अविश्वसनीय रूप से अच्छा काम करता है)। –

1
ContentResolver cr = mContext.getContentResolver(); Cursor cursor= mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, "HAS_PHONE_NUMBER <> 0", null, null); if (cursor!= null) { final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); final int numberIndex = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); final int idIndex= cursor.getColumnIndex(ContactsContract.Contacts._ID); String displayName, number = null, idValue; while (cursor.moveToNext()) { 
    displayName = cursor.getString(displayNameIndex); 
    idValue= cursor.getString(idIndex); 
    Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "contact_id = '" + idValue + "'", null, null); 
    phones.moveToFirst(); 
    try { 
    number = phones.getString(phones.getColumnIndex("data1")); 
    } 
    catch (CursorIndexOutOfBoundsException e) 
    {` 
    } 
    phones.close(); 
    userList.add(new ContactModel(displayName , number , null , } 
संबंधित मुद्दे