我是 android 的新手,我想获取联系人并在列表视图中显示。这是我在 asynctask 中获取联系人的代码。请帮助如何在获取每个联系人后更新列表视图。
protected Void doInBackground(Void... params) {
ContentResolver cr = ReadContactsActivity.this.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//li.add("Name:"+name+",Number:"+phone);
publishProgress("Name:"+name+",Number:"+phone);
}
pCur.close();
}
}
}
return null;
}
// A callback method executed on UI thread, invoked by the publishProgress()
// from doInBackground() method
@Override
protected void onProgressUpdate(String... values) {
list=(ListView) findViewById(R.id.lvDisplay);
ArrayAdapter<String> adp=new ArrayAdapter<String>
(getBaseContext(),R.layout.list,R.id.label,li);
list.setAdapter(adp);
}