2017-11-23 2 views
0

Ich entwickle eine Kontakt-App mit drei Fragmenten (callLog, contactList, favoriteContactList). Wenn Sie eines zu einem anderen Fragment verschieben, benötigt es Zeit, alle Kontakte abzurufen. Ich habe ca. 276 Kontakte auf meinem Gerät. Jede Lösung, die Zeit reduziert.Die beste Methode zum Abrufen der Kontaktliste

ContentResolver cr = getContext().getContentResolver(); 
     String[] projection = {ContactsContract.Contacts._ID, 
       ContactsContract.Contacts.DISPLAY_NAME, 
       ContactsContract.Contacts.PHOTO_URI, 
       ContactsContract.Contacts.HAS_PHONE_NUMBER}; 

     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
     String id, name, image_uri, phone = ""; 

     if (cur != null && cur.getCount() > 0) { 
      while (cur.moveToNext()) { 

       id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
       name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       image_uri = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)); 

       Log.d(TAG, " name " + name + " id " + id); 

       // phone number 
       if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); 
        if (pCur != null && pCur.moveToNext()) { 

         phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         int colIndex = pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); 
         int lblIndex = pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL); 
         int labelType = pCur.getInt(colIndex); 
         String phoneType; 

         if (labelType == ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM) { 
          phoneType = pCur.getString(colIndex); 
         } else { 
          phoneType = String.valueOf(ContactsContract.CommonDataKinds.Phone.getTypeLabel(this.getResources(), labelType, "Mobile")); 
         } 

         Log.d(TAG, phoneType + " " + phone); 
         pCur.close(); 
        } 
        mContactArrayList.add(new Contact(id, name, phone, image_uri)); 
       } 
      } 
      cur.close(); 
     } 
+0

Rufen Sie diese Methode auf separaten Thread (AsyncTask). – ADM

Antwort

2

Versuchen Sie diesen Code

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
    while (phones.moveToNext()) { 
     String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     String phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 
     String image_uri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)); 

     mContactArrayList.add(new Contact(id, name, phone, image_uri)); 

    } 

Ich denke, es funktioniert.

+0

es funktioniert, aber manchmal Cursor gibt doppelte Wert zurück. –

+0

Ja, weil es alle Kontakte aus Ihrer Kontaktdatenbank bekommt. Deshalb gibt – SSALPHAX

+0

einen Eintrag per * Telefonnummer * in der DB zurück, nicht per * contact *. Sie müssen mContactArrayList als HashMap ändern und mehrere Telefonnummern für denselben Kontakt in dasselbe Kontaktobjekt einfügen. Weitere Informationen finden Sie in dieser Antwort: https://stackoverflow.com/a/46594049/819355 – marmor

Verwandte Themen