这里显示我的联系人名称列表。通过单击列表,我可以得到ContactName
和Contact Id
。从中,我想要通过使用Contact ID
或Contact name
来获取电话号码
,请帮助我。
以下是我的代码
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();
}