Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fetch Contacts into the Application

I am trying to show a list of Contacts from the users phone in a List view inside the application. I am able to fetch the contacts but some of the Contacts will have mutiple cellphone numbers so i want to show that person that many number of 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();

Want to Display a user as many times as the number of numbers he has. Will i be even able to short list it based of only cellphone numbers which is 10 digit long ?

Example

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

Name: Sarah 
Number 1: xxxxxxxxx

This should return be three List Items as Follows

John Doe  xxxxxxxxx
John Doe  xxxxxxxxx
Sarah     xxxxxxxxx
like image 844
Harsha M V Avatar asked Oct 21 '22 12:10

Harsha M V


1 Answers

You might 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;
   }
}
like image 154
mach Avatar answered Oct 29 '22 20:10

mach