I was following these links to get the contacts in my application
How to call Android contacts list?
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
I can display the list of contacts on phone but
-
I want to add a checkbox at each contact so that user can select multiple contacts and by clicking the done button he should be able to get all the contacts he selected
Also I want to get the name of contact as well as the phone number of contact , see my code :
if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = managedQuery(contactData, null, null, null, null); if (c.moveToFirst()) { String name = c.getString(c .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER)); Log.v("name", name +" "+number); // TODO Whatever you want to do with the selected contact // name. } }
on log cat it shows output as :
01-09 12:46:41.688: V/name(699): Xyz 1
that is the name of contact is xyz and it has atleast 1 phone number associated with it.Please help me on how can i get the number associated with that contact.
EDIT :
if i use this(String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER));
) line in code the I get following exception :
01-09 13:33:23.008: E/AndroidRuntime(786): FATAL EXCEPTION: main
01-09 13:33:23.008: E/AndroidRuntime(786): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-2C2E30/1 (has extras) }} to activity {sra.com/sra.com.ContactsDemo}: java.lang.IllegalArgumentException: column 'number' does not exist
01-09 13:33:23.008: E/AndroidRuntime(786): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
01-09 13:33:23.008: E/AndroidRuntime(786): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
01-09 13:33:23.008: E/AndroidRuntime(786): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
01-09 13:33:23.008: E/AndroidRuntime(786): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
01-09 13:33:23.008: E/AndroidRuntime(786): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 13:33:23.008: E/AndroidRuntime(786): at android.os.Looper.loop(Looper.java:123)
01-09 13:33:23.008: E/AndroidRuntime(786): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-09 13:33:23.008: E/AndroidRuntime(786): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 13:33:23.008: E/AndroidRuntime(786): at java.lang.reflect.Method.invoke(Method.java:521)
01-09 13:33:23.008: E/AndroidRuntime(786): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-09 13:33:23.008: E/AndroidRuntime(786): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-09 13:33:23.008: E/AndroidRuntime(786): at dalvik.system.NativeStart.main(Native Method)
01-09 13:33:23.008: E/AndroidRuntime(786): Caused by: java.lang.IllegalArgumentException: column 'number' does not exist
01-09 13:33:23.008: E/AndroidRuntime(786): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
01-09 13:33:23.008: E/AndroidRuntime(786): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
01-09 13:33:23.008: E/AndroidRuntime(786): at sra.com.ContactsDemo.onActivityResult(ContactsDemo.java:49)
01-09 13:33:23.008: E/AndroidRuntime(786): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
01-09 13:33:23.008: E/AndroidRuntime(786): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
01-09 13:33:23.008: E/AndroidRuntime(786): ... 11 more
In here you have got the cursor c
to get the name
to get the number
You can iterate through the cursor and save them to a list. Create a checkbox list view and bind with that list.
Please try this code
By seeing the answers, I think you got the answer how to fetch contacts and now you want to get the selected contacts on your activity.
Note: Here
contactName
is the contact name of which you want to fecth contact numbers.1. Start your contact activity with
startActivityForResult()
.2. Initialize
ArrayList
variable in contact activity say itcontactArrayList
.3. When user
checks the checkbox
, add this contact in yourcontactArrayList
and keep on adding and whenunchecks
then remove the contact from thecontactArrayList
.4. When user presses done then set the result ok with the selected contact list which you have added in
contactArrayList
like this:and
finish()
this activity.5. On your calling activity override:
Note: The above code was tested over 2.3.3.
Try the code below:
Simply, have a contact data object, and whenever you select any item in list, set value of that contact into this field.
in