我正在尝试在我自己的列表视图中检索所有联系人姓名及其号码。我想获得所有名字,但是当我想获得电话号码时,它会在每次联系时向我显示相同的号码。
其中数字从HAS_PHONE_NUMBER获得值1
我的代码是
if (number > 0) {
Cursor phones = managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID ,
null, null);
startManagingCursor(phones);
phones.moveToFirst();
String cNumber = phones.phones.getString(phones.getColumnIndex("data1"));
cache.nameView.setText(cache.nameBuffer.data, 0, size);
cache.numView.setText(cNumber);
}
提前致谢..
答案 0 :(得分:0)
试试这个:
//get all contacts
Cursor peopleCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null,null, null);
if(peopleCursor.getCount()>0)
{
peopleCursor.moveToFirst();
for(int i=0;i<peopleCursor.getCount();i++)
{
if(check for HAS_PHONE_NUMBER)
{
//get number
Cursor numberCursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},ContactsContract.CommonDataKinds.Phone._ID+"="+peopleCursor.getString(peopleCursor.getColumnIndex(ContactsContract.Contacts._ID)), null,null);
numberCursor.moveToFirst();
String number=numberCursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//get name
String name=peopleCursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
peopleCursor.moveToNext();
}
}
}
答案 1 :(得分:0)
你必须设置While或for循环。对于你使用的代码,如果它基本上用于不能增加数值varable值的条件。 计算您获取的名称总数 它的变量名称为totalNumber_name
if (number == totalNumber_name) {
Cursor phones = managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID ,
null, null);
startManagingCursor(phones);
phones.moveToFirst();
String cNumber = phones.phones.getString(phones.getColumnIndex("data1"));
cache.nameView.setText(cache.nameBuffer.data, 0, size);
cache.numView.setText(cNumber);
number++;
}
可能是它的工作
答案 2 :(得分:0)
尝试使用此代码,它可以在我的应用程序中正常运行。
while(c.moveToNext())
{
contactName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactID },null);
while (pCur.moveToNext()) {
contactTelNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
Log.i("name ", contactName + " ");
Log.i("number ", contactTelNumber + " ");