我試圖檢索帶有姓名和電話號碼的聯繫人列表。我嘗試下面的代碼:如何檢索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