0

I am using this code below to pick a contact number , but some contacts have more than one number, how can I select one of the contact numbers?

Cselect.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
              startActivityForResult(intent, 0);     
            }
            });
Glenn Sallis
  • 415
  • 3
  • 11
majed
  • 29
  • 8

1 Answers1

1

In the result, you should get a Uri. From this, you will be able to grab a Cursor, and then iterate over the cursor to grab the information you require. I would advise you dump the cursor into the Logs so you can see what is returned, by using the DatabaseUtils class.

The following is a snippet of how you can retrieve the Cursor and iterate over it:

    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(People.NAME));
            txtContacts.setText(name);
        }
    }
Mimminito
  • 2,803
  • 3
  • 21
  • 27
  • hi . i want to let the user selects the number , not me – majed Mar 19 '12 at 14:47
  • Then I all you need to do is display your own AlertDialog with the numbers you have retrieved from the Cursor above, and the user can select from that? – Mimminito Mar 19 '12 at 15:24
  • if you don't mind , can you help me with it ... how to do the dialog? here is my code http://pastebin.com/8UxwdV4g – majed Mar 20 '12 at 14:10
  • Once you have the phone numbers you want to display, just create an AlertDialog with a List attached. The code for this is shown in the docs http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList – Mimminito Mar 20 '12 at 15:09
  • yes but i mean how to get the numbers ? i tried: int phoneIdx = c.getColumnIndex(name); String phoneNumber = c.getString(phoneIdx); - name - is what i got from cursor name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); you got me ? i am sorry but i am not that good with cursors – majed Mar 20 '12 at 16:14
  • Please refer to the following post for how to get the numbers (just search StackOverflow, this has been asked multiple times) and then user while(cursor.moveToNext()) to get the phone numbers http://stackoverflow.com/questions/2356084/read-all-contacts-phone-numbers-in-android – Mimminito Mar 20 '12 at 17:24
  • i am sorry but i got this error - get field slot from row 0 col -1 failed - from this - phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); - – majed Mar 21 '12 at 16:28
  • It would seem that you have not read how cursors work, or how to use them properly. Rather than just paste code so you can copy it, I am going to give you this link so you can debug this error yourself http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/2/ – Mimminito Mar 21 '12 at 16:46