gpt4 book ai didi

java - 获取联系人错误 - 如何解决?

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:35 25 4
gpt4 key购买 nike

我的问题是,我列出了联系人,电话号码显示得很好,但我无法使用姓名。

我尝试了多种方法来获取“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" />

关于java - 获取联系人错误 - 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56315549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com