Android Fetch Contacts in app

I am trying to show the contact list from the user's phone in the List view inside the application. I can get contacts, but some of the contacts will have several cell phone numbers, so I want to show this person many times.

Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    String name, number = "";
    String id;
    c.moveToFirst();
    for (int i = 0; i < c.getCount(); i++) {
        name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

        if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id },
                    null);
            while (pCur.moveToNext()) {
                number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
        }
        Log.i("name ", name + " ");
        Log.i("number ", number + " ");
        c.moveToNext();

You want to display the user as many times as the number of his numbers. Will I even be able to briefly list it based on only phone numbers whose length is 10 digits?

Example

Name: John Doe 
Number 1: xxxxxxxxx
Number 2: xxxxxxxxx

Name: Sarah 
Number 1: xxxxxxxxx

It should be three list items as follows

John Doe  xxxxxxxxx
John Doe  xxxxxxxxx
Sarah     xxxxxxxxx
+5
source share
2 answers

You can try something like this

List<PhoneItem> phoneNoList = new ArrayList<PhoneItem();
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String name, number = "";
String id;
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
    name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

    if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
        Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id },
                null);
        while (pCur.moveToNext()) {
            phoneNoList.add(new PhoneItem(name, pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))));
        }
    }
    c.moveToNext();


}

for (PhoneItem row : phoneNoList) {
    Log.i("name", row.name);
    Log.i("number", row.number+"");
}

[...]

private class PhoneItem {
   String name;
   String phone;

   public PhoneItem(String name, String phone) {
       this.name = name;
       this.phone = phone;
   }
}
+2
source

. , . .

String[] projection = new String[]{ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                    ContactsContract.CommonDataKinds.Phone.NUMBER,};
String selection = ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "=?" ;  
String[] selectionArgs = new String[] {"1"};                            
Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                    projection, 
                                    selection, 
                                    selectionArgs, 
                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                                    + ", " + ContactsContract.CommonDataKinds.Phone.NUMBER);
+1

All Articles