我的问题是我正在列出联系人,电话号码显示的很好,但是我却无法使用姓名。
我尝试了几种获取“ DISPLAY_NAME”的方法,但没有一个起作用
private void initializeRecyclerView() {
mUserList = findViewById(R.id.userList);
mUserList.setNestedScrollingEnabled(false);
mUserList.setHasFixedSize(false);
mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false);
mUserList.setLayoutManager(mUserListLayoutManager);
mUserListAdapter = new UserListAdapter(userList);
mUserList.setAdapter(mUserListAdapter);
getContactsList();
}
This is my adaptor ^^
private void getContactsList(){
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while(phones.moveToNext()){
String phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
UserObject mContact = new UserObject(name, phone);
userList.add(mContact);
mUserListAdapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
我正在与此联系:-
private void getContactsList(){
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while(phones.moveToNext()){
String phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
UserObject mContact = new UserObject(name, phone);
userList.add(mContact);
}
mUserListAdapter = new UserListAdapter(userList);
mUserList.setAdapter(mUserListAdapter);
}
,并设置获取联系人的权限:-
<uses-permission android:name="android.permission.READ_CONTACTS" />
答案 1 :(得分:0)
您需要添加PROJECTION
您可以将此方法放入代码中
public Cursor getContact(Context context) {
String[] PROJECTION = new String[]{
ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
Uri uri = CommonDataKinds.Phone.CONTENT_URI;
String order = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";// LIMIT " + limit + " offset " + lastId + "";
try {
return context.getContentResolver().query(uri, PROJECTION, null, null, order);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
并使用
获取名称ContactsContract.Contacts.DISPLAY_NAME
快乐编码