【发布时间】:2019-07-06 21:14:26
【问题描述】:
我正在使用Arraylist 来获取我的应用程序中的所有可用联系人。这效率不高,因为Arraylist 需要很长时间才能获取和填充Listview,因为几乎有600+ contacts。
我正在寻找一种性能更好的替代方法。
虽然我搜索了其他相关问题,但我找不到方便的问题。
这是我的java代码:
private List<String> getContactList() {
List<String> stringList=new ArrayList<>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME)
);
if (cur.getInt(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 phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.v("Data : ",""+id+" "+name+" "+phoneNo);
stringList.add(id);
stringList.add(name);
stringList.add(phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
return stringList;
}
【问题讨论】:
-
ArrayList在这里不是问题,它只是一个Collection。ContentResolver查询需要时间,因此您最好在工作线程上执行此操作。而ContentResolver确实支持分页,因此您还可以逐页加载数据.. -
检查这个 - stackoverflow.com/a/51064521/7649582 。也许它会帮助你。
-
请检查stackoverflow.com/questions/10844672/…,如果这能解决您的查询,您的实际问题是从数组列表中获取
contacts获取对象不是问题。
标签: java android listview arraylist android-contacts