【发布时间】:2013-02-07 06:41:29
【问题描述】:
当我尝试从电话的联系人列表中获取电话号码时。问题是,当我在手机中的联系人列表为空时运行应用程序时,应用程序停止了。我查了一下,这是因为光标是空的。
如何查看光标是否为空或者手机的联系人列表中是否有联系人?
ArrayList<String> lstPhoneNumber = new ArrayList<String>();
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
lstPhoneNumber = new ArrayList<String>();
phones.moveToFirst();
// The problematic Line:
lstPhoneNumber.add(phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)));
while (phones.moveToNext()) {
lstPhoneNumber.add(phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
【问题讨论】:
-
摆脱 phones.moveToFirst() 和 1stPhonenumber.add 呼叫.. 离开你的 while 循环。固定。
-
另外,尝试传入您要查询的列的投影。当您只需要 1 时,没有用检索所有列(通过为投影传递 null)。
标签: android android-contacts android-cursor