How do I change contact numbers to contact names?

Is there a way to display the contact names instead of the contact numbers?

This method returns the phone numbers within an inbox, but I need the names instead:

UPDATE: code changed from the original.

NOTE: I'm trying to change the contact numbers to display their corresponding contact names in the INBOX. Im not trying to get a list of all available contacts names.

public ArrayList<String> fetchInboxNumbers() {

    ArrayList<String> sms = new ArrayList<String>();
    Uri uriSms = Uri.parse("content://sms/inbox");

    Cursor cursor = getContentResolver().query(uriSms,
            new String[] { "_id", "address", "date", "body" }, null, null,                                                              
            null);

    cursor.moveToFirst();
    while (cursor.moveToNext()) {

        String address = cursor.getString(1); // Displays phone number

        sms.add(address);
    }
    return sms;
} // END FETCHINBOX

Answers


Is there a way to display the contact names instead of the contact numbers?

Following Snippet will help you.

        String whereName = ContactsContract.Data.MIMETYPE + " = ?";
        String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
        Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
        while (nameCur.moveToNext()) {
            String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
            String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
            String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
        }
        nameCur.close();

I will like to add to @Vipul Shah answer.

wouldnt be better to add a projection with only the columns that you need to retreive?

new String [] { ContactsContract.CommonDataKinds.Phone._ID, 
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }

Regards


Need Your Help

Generating a canonical path

java path

Does any one know of any Java libraries I could use to generate canonical paths (basically remove back-references).

SQL Query NOT Between Two Dates

sql between

I need some help with SQL Query.