我试图检索带有姓名和电话号码的联系人列表。我尝试下面的代码:如何检索Android中的联系人姓名和电话号码
// Get a cursor over every contact.
Cursor cursor = getContentResolver().query(People.CONTENT_URI,
null, null, null, null);
// Let the activity manage the cursor lifecycle.
startManagingCursor(cursor);
// Use the convenience properties to get the index of the columns
int nameIdx = cursor.getColumnIndexOrThrow(People.NAME);
int phoneIdx = cursor. getColumnIndexOrThrow(People.NUMBER);
String[] result = new String[cursor.getCount()];
if (cursor.moveToFirst())
do {
// Extract the name.
String name = cursor.getString(nameIdx);
// Extract the phone number.
String phone = cursor.getString(phoneIdx);
result[cursor.getPosition()] = name + "-" +" "+ phone;
} while(cursor.moveToNext());
此代码应与所有联系人的名字和它的电话号码,但这个仅返回联系人的姓名返回一个数组,并返回电话号码为NULL,
输出示例:
John - null
我现在无法模拟你的问题,但想问你得到的phoneIdx是什么?你有没有在联系数据库中检查这些字段是否存在? – sat 2011-01-06 08:46:14