Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Specific Contact Number by using Contact Id

Here my Contact names are Displayed on List View . By clicking the List I get ContactName and Contact Id. From that I want to fetchPhone number by Using either Contact ID or Contact name,Please Help me.

Here is My Code

void ReadContacts(String sort) {
    final Uri uri = ContactsContract.Contacts.CONTENT_URI;
    final String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME
    };
    //boolean mShowInvisible = false;
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
    String[] selectionArgs = null;
    final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    m_curContacts = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};
    m_slvAdapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, 
                m_curContacts,
                fields, 
                new int[] {android.R.id.text1});
    m_slvAdapter.setFilterQueryProvider(new FilterQueryProvider() {

        public Cursor runQuery(CharSequence constraint) {
            Log.d(LOG_TAG, "runQuery constraint:"+constraint);
            String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'" +
                    " AND "+ ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%"+constraint+"%'";
            String[] selectionArgs = null;//new String[]{"'1'"};//, };
            Cursor cur = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
            return cur;
        }

   });
   m_lvContacts.setAdapter(m_slvAdapter); 
   //  cur.close();

}



public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
    ContentResolver cr;
    Cursor cursor = (Cursor) m_lvContacts.getItemAtPosition(position);
    String szDisplayName = cursor.getString(cursor.getColumnIndexOrThrow( ContactsContract.Contacts.DISPLAY_NAME));
    String szId = cursor.getString(cursor.getColumnIndexOrThrow( ContactsContract.Contacts._ID));
    int nId = cursor.getInt(cursor.getColumnIndexOrThrow( ContactsContract.Contacts._ID));
    Log.d(LOG_TAG, "Item click:"+position+" szId:"+szId+" nId:"+nId+" Data:"+szDisplayName);
    Toast.makeText(getBaseContext(), "Item click:"+phoneNumber+" szId:"+szId+" nId:"+nId+" Data:"+szDisplayName, Toast.LENGTH_SHORT).show();

}
like image 421
user933909 Avatar asked Mar 10 '12 07:03

user933909


2 Answers

Try this one

ArrayList<String> phones = new ArrayList<String>();

Cursor cursor = mContentResolver.query(
        CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null);

while (cursor.moveToNext()) 
{
    phones.add(cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER)));
} 

cursor.close();
return(phones);
like image 149
Dhaval Avatar answered Oct 05 '22 08:10

Dhaval


Try this one

public void getNameUsingContactId(String contactId){

    String cContactIdString = ContactsContract.Contacts._ID;
    Uri cCONTACT_CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
    String cDisplayNameColumn = ContactsContract.Contacts.DISPLAY_NAME;

    String selection = cContactIdString + " = ? ";
    String[] selectionArgs = new String[]{String.valueOf(contactId)};

    Cursor cursor = mContext.getContentResolver().query(cCONTACT_CONTENT_URI, null, selection, selectionArgs, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();
        while ((cursor != null) && (cursor.isAfterLast() == false)) {
            if (cursor.getColumnIndex(cContactIdString) >= 0) {
                if (contactId.equals(cursor.getString(cursor.getColumnIndex(cContactIdString)))) {
                    String name = cursor.getString(cursor.getColumnIndex(cDisplayNameColumn));
                     break;
                }
            }
            cursor.moveToNext();
        }
    }
    if (cursor != null)
        cursor.close();  
}   
like image 42
Siddarth Kanted Avatar answered Oct 05 '22 07:10

Siddarth Kanted