您好我想从android中的联系人列表中检索姓名和号码,我使用下面的代码,它会给我姓名,但不是号码,它给出数字的空值
注意:我正在使用Android模拟器
public void demo(){
String phoneNumber;
String [] item = new String[]{ People._ID,
People.NAME,
People.NUMBER};
Cursor cur = getContentResolver().query(People.CONTENT_URI ,item , null, null, null);
int phoneNumberIndex = cur.getColumnIndexOrThrow(People.NUMBER);
if (cur != null) {
Log.v("cur not null", "Cursor Not null");
if (cur.moveToNext()) {
Log.v("moveToNext", "Moved to first");
Log.v("moveToNext", "Cursor Moved to first and checking");
phoneNumber = cur.getString(phoneNumberIndex);
System.out.println("****** from_number "+phoneNumber +" *****************");
}
}
}
答案 0 :(得分:0)
它给出null,因为你试图只检索一个数字..这应该有用..
if (cur.moveToNext()) {
Log.v("moveToNext", "Moved to first");
Log.v("moveToNext", "Cursor Moved to first and checking");
while(cur.moveToNext())
{
phoneNumber = cur.getString(phoneNumberIndex);
System.out.println("****** from_number "+phoneNumber +" *****************");
}
}