Search code examples
androidlistviewandroid-listviewcontactscontractandroid-adapterview

How to retrieve data from contact Uri of a list item obtained from onItemClick() in listview?


How do I obtain the contact details from a uri in a list view? . I have obtained the contact uri for the selected contact from the listview. The following snippet is a what I have attempted but does not work. What's even more surprising is that the Logging statements don't even execute!

@Override
public void onItemClick(
    AdapterView<?> parent, View item, int position, long rowID) {
    // Get the Cursor
    Cursor cursor = ((SimpleCursorAdapter) parent.getAdapter()).getCursor();
    // Move to the selected contact
    cursor.moveToPosition(position);
    // Get the _ID value
    mContactId = cursor.getLong(CONTACT_ID_INDEX);
    // Get the selected LOOKUP KEY
    mContactKey = cursor.getString(CONTACT_KEY_INDEX);
    // Create the contact's content Uri
    mContactUri = Contacts.getLookupUri(mContactId, mContactKey);
    // ListView Clicked item index

    String id=new String();
    String name = new String();
    String hasPhone = new String();
    int idx;

    Cursor cursor1 = getActivity().getContentResolver().query(mContactUri, null, null, null, null);
    {
        idx = cursor1.getColumnIndex(ContactsContract.Contacts._ID);
        id = cursor1.getString(idx);

        idx = cursor1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        name = cursor1.getString(idx);

        idx = cursor1.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        hasPhone = cursor1.getString(idx);
    }
    Log.i("Phone Number","The phone number is: "+hasPhone);
    Log.i("Name","The name is: "+name);
    /*
     * You can use mContactUri as the content URI for retrieving
     * the details for a contact.
     */
}

This is the Log for the particular time the listview item was clicked.

09-05 21:55:07.663: I/am_on_resume_called(25140): [0,com.laer.okgo.PersonListActivity]
09-05 21:55:07.738: W/Adreno-GSL(25140): <ioctl_kgsl_device_getproperty:663>: mmap failed: errno 22 Invalid argument
09-05 21:55:07.738: I/Adreno-EGL(25140): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I10246dbd022c719c705be805d5642cc8fdfbd2a2Date: 03/10/14
09-05 21:55:07.739: I/CanvasContext(25140): Initialized EGL, version 1.4
09-05 21:55:13.932: I/ImageButton(25140): Clicked!!

This is where the list view shows after it is started by an ImageButton

09-05 21:55:13.941: I/am_on_paused_called(25140): [0,com.laer.okgo.PersonListActivity]

This is is when I click a list item.

09-05 21:55:17.825: I/am_on_resume_called(25140): [0,com.laer.okgo.PersonListActivity]

Also a bonus question. How do i change the theme/styling for a listview? It would be nice if someone can point me in the right direction!


Solution

  • Now this was really my fault. I was clicking the wrong UI element. There were two buttons with similar names. I didn't realize that. Lol I screwed up big time! Wasted a lot of time... So moral of the story keep your UI/UX elements with distinct and easily distinguishable names!