2

Here is my code which give me the contact's name and phone number by click on list item:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    final String contactName = cursor.getString(cursor.getColumnIndexOrThrow("DISPLAY_NAME"));
    Cursor contCursor =(Cursor) listStarred.getItemAtPosition(position);
    String strid = contCursor.getString(contCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

    ContentResolver cr = getContentResolver();

    String name = null;
    String lname ="...";

    contCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"='"+strid+"'",
            null, null);

    int phoneNumberIndex = contCursor
            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
    Log.d("Count", String.valueOf(contCursor.getCount()));

    if (contCursor != null) {
        Log.v("CurNotNull", "Cursor Not null");
        try {
            if (contCursor.moveToNext()) {
                Log.v("MoveToFirst", "Moved to first");
                Log.v("CheckMTF", "Cursor Moved to first and checking");
                lname = contCursor.getString(phoneNumberIndex);
                Toast.makeText(getApplicationContext(), contactName + " " + lname, Toast.LENGTH_SHORT).show();
            }
        } finally {
            Log.v("Finally", "In finally");
            contCursor.close();
        }
}
}

The above code seems that works fine, but how can instead of get one phone number to get a list with all contact's numbers? What changes need to be done to get an array with the numbers(and type of each) for the selected contact?

1 Answer 1

0

The user which answered deleted his answer, but he actually help me with the code he/she provided. I was able to get the other numbers using a part of the code provided. So here is the improved code:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    final String contactName = cursor.getString(cursor.getColumnIndexOrThrow("DISPLAY_NAME"));
    contCursor =(Cursor) listStarred.getItemAtPosition(position);
    String strid = contCursor.getString(contCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

    ContentResolver cr = getContentResolver();

    ArrayList<String> phones = new ArrayList<String>();
    contCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"='"+strid+"'",
            null, null);

    while (contCursor.moveToNext()){
        String phoneNo = contCursor.getString(contCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Toast.makeText(getApplicationContext(), "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();

    }
}

Now just need to save the numbers on an array or list to populate a listview.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.