我的问题是我正在列出联系人,电话号码显示得很好,但是我无法工作。
我尝试了几种获得" 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();
}
}
我正在获得与此联系的联系: -
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" />
您需要添加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
快乐编码