以下是我的代码,实际上在屏幕上它没有显示任何联系人。在模拟器中我添加了5个联系人。请告诉我该怎么做。
{
//some code
Cursor cur = getContacts();
String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this,
R.layout.list_view_item_new,
cur,
fields,
new int[] {R.id.contactEntryText});
lv.setAdapter(adapter);
}
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection =
new String[]{ ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String selection = null;
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME +
" COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
答案 0 :(得分:5)
我复制并执行了几乎相同的代码并且它可以工作:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor cur = getContacts();
ListView lv = getListView();
String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this,
R.layout.main,
cur,
fields,
new int[] {R.id.txtbox});
lv.setAdapter(adapter);
}
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection =
new String[]{ ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String selection = null;
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME +
" COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
请检查textview实施中是否有任何错误?
答案 1 :(得分:1)
首先,我只是缩小问题范围。
1)检查您是否有阅读联系人的权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
2)检查光标是否有任何结果
cur.getCount()