0

I am making an app to upload the phonebook of an android device on server side. I am able to get the names in the phonebook but I am not getting the contact numbers of respective names.

I am using People.NUMBER method for getting the no of a contact.

DonGru
  • 13,532
  • 8
  • 45
  • 55
aditya
  • 1

2 Answers2

0

use this code. this code stores the Contant name and number in the Arralist.

ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
ContentResolver cr = getContentResolver();
         Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
         while (cursor.moveToNext()) {
             try{
             String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
             String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
             String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
             if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                 Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
                 while (phones.moveToNext()) { 
                     String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                     HashMap<String,String> map=new HashMap<String,String>();
                     map.put("name", name);
                     map.put("number", phoneNumber);
                     contactData.add(map);
                 } 
                 phones.close(); 
             }
         }catch(Exception e){}
         }

Use the contactData list as you need.

Balaji.K
  • 8,745
  • 5
  • 30
  • 39