Android Studio编程cursor.getColumnIndex报错Value must be ≥ 0 but getColumnIndex
can be -1怎么解决
private void setData() {
List<ContactInfo> contactInfos = getContacts();
ContactAdapter adapter = new ContactAdapter(ContactActivity.this, contactInfos);
rv_contact.setAdapter(adapter);
}
public List<ContactInfo> getContacts() {
List<ContactInfo> contactInfos = new ArrayList<>();
Cursor cursor = getContentResolver().query(ContactsContract.
Contacts.CONTENT_URI, null,null, null, null);
if (contactInfos!= null)contactInfos.clear();
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.
Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.
Contacts.DISPLAY_NAME));
int isHas = Integer.parseInt(cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (isHas > 0) {
Cursor c = getContentResolver().query(ContactsContract.
CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +
"=" + id, null, null);
while (c.moveToNext()) {
ContactInfo info = new ContactInfo();
info.setContactName(name);
String number = c.getString(c.getColumnIndex(ContactsContract.
CommonDataKinds.Phone.NUMBER)).trim();
number = number.replace(" ", "");
number = number.replace("-", "");
info.setPhoneNumber(number);
contactInfos.add(info);
}
c.close();
}
}
cursor.close();
return contactInfos;
}
是不是表中没有数据啊。