我只是想访问联系内容提供商。因为我写了以下代码
Cursor cur= getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,null,null,null);
startManagingCursor(cur);
String[] result=new String[cur.getCount()];
if(cur.moveToFirst())
{
int nameidx=cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int Ididx=cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
String strName=cur.getString(nameidx);
String strId=cur.getString(Ididx);
result[cur.getPosition()]=strName+"("+strId+")";
}while(cur.moveToNext());
stopManagingCursor(cur);
}
但它只显示一个联系人姓名我想显示所有联系人,请告诉我这是怎么做的。
答案 0 :(得分:1)
试试这个
Cursor cur= getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,null,null,null);
startManagingCursor(cur);
String[] result=new String[cur.getCount()];
for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext())
{
int nameidx=cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int Ididx=cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
String strName=cur.getString(nameidx);
String strId=cur.getString(Ididx);
result[cur.getPosition()]=strName+"("+strId+")";
}
stopManagingCursor(cur);
}