您好我现在制作了Fragment
我试图将所有联系人姓名显示给我的ListView
我做了所有事情,但我仍然得到Cursor
null我也打电话给{{} 1}}。仍然是错误相同。
getActivity()
例外:
public class AllContacts extends Fragment {
public AllContacts() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_contacts, container, false);
ListView list = (ListView) view.findViewById(R.id.listview);
ArrayList<String> arraylist= new ArrayList<>();
String sorting = ContactsContract.Contacts.DISPLAY_NAME+ "DESC";
Uri uri = Uri.parse("content://ContactsContract");
Cursor c = getActivity().getContentResolver().query(uri,null,null,null,sorting);
while (c.moveToNext()) {
String contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
arraylist.add(contactName);
}//end loop
c.close();
ArrayAdapter adapter = new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,arraylist);
list.setAdapter(adapter);
return view;
}
}
答案 0 :(得分:3)
您的查询存在两个问题,第一个问题是DESC
关键字之前缺少空格,第二个问题是无效Uri
。
更改以下内容:
String sorting = ContactsContract.Contacts.DISPLAY_NAME+ "DESC";
Uri uri = Uri.parse("content://ContactsContract");
Cursor c = getActivity().getContentResolver().query(uri,null,null,null,sorting);
对此:
String sorting = ContactsContract.Contacts.DISPLAY_NAME + " DESC";
Cursor c = getActivity().getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, sorting);