这是我的代码,通过点击列表项目给我联系人的姓名和电话号码:
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();
}
}
}
上面的代码似乎运行正常,但是如何才能获得一个电话号码来获取包含所有联系人号码的列表? 为获得所选联系人的数字(和每个类型)的数组需要做哪些更改?
答案 0 :(得分:0)
回答的用户删除了他的答案,但他实际上帮助我提供了他/她提供的代码。 我能够使用提供的部分代码获取其他数字。所以这是改进的代码:
@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();
}
}
现在只需要将数字保存在数组或列表中以填充列表视图。