0

Is it possible to read more than one contact from the contact list from my app.?? what i found is how to receive a list of contacts and loop through the list.

How to read contacts on Android 2.0

but is it possible to select a few of them from the list of contacts.do i need to create a seperate layout for selecting the contacts of my choice and load that layout with data from the contacts list?? please help.

Community
  • 1
  • 1
scooby
  • 493
  • 11
  • 31
  • It looks like you are talking about two different things: Are you talking about programmatically reading contacts from the ContactsContractProvider? Or are you talking about picking contacts using the CONTACT_PICK intent? – Vikram Bodicherla Jan 27 '12 at 05:48

2 Answers2

0

Obviously you are able to select contacts of your choice from list of contacts.But you just need to find a proper "where" clause in query(i.e. what type of contacts you want from list).

And yes,you need to create separate layout to show them up.

For Example: Getting contacts with phone number

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.Contacts.HAS_PHONE_NUMBER+"='true'", null, null);  // gives you the list of contacts who has phone numbers

while (cursor.moveToNext()) { 

     String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
     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));                 
      }
      phones.close(); 
}

Hope,i get your requirement correctly.If not,please let me know!

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • actually its not about the 'where' clause.what i want is to select some of the contacts with the help of checkboxes,(provided by the side of the contacts in the new xml.)do you think this can be done in the same way??? – scooby Jan 27 '12 at 05:08
  • So you mean,you are populating all contacts in one screen with a checkbox for each of them and you want to reselect the contacts for which,checkbox is checked?? – Hiral Vadodaria Jan 27 '12 at 05:17
  • If so,then you need to store ids of selected contacts in an array and then query them from ContentResolver again to populate them in listview. – Hiral Vadodaria Jan 27 '12 at 05:24
  • yaa. thats what i want to do.. reselect the contacts for which the checkboxes are checked. in simple terms i got what you wanted to say.. – scooby Jan 27 '12 at 09:22
  • Have you made any custom adapter class to populate your listview with contacts? – Hiral Vadodaria Jan 27 '12 at 09:48
-1
  1. Basically you need to iterate through all contacts with their unique id called contact_id.

  2. Avoid using cursors inside cursors in the loop.

try this demo app. see how quickly it reads contacts in a proper android way

Source code attached

Prasanna Anbazhagan
  • 1,693
  • 1
  • 20
  • 37