得到的接触性能问题 [英] get contacts performance issue
问题描述
我试图让Android设备的联系人。
我发现了一个很大的样品code的这里
然而,试图填充 ListActivity
与 ArrayAdapter
正在使用code它走的时候很多时候 - 银河S2将近4秒,旧设备上的多得多。
我需要提高其性能。我以为是工具光标
,将举行多个维度光标
,并使用 SimpleCursorAdapter
作为列表适配器。结果
我看到一些问题,这种方法:
- 我不知道如何在特定位置获得的项目。
- 每个光标具有不同的列。
有没有更好/更简单的方法来做到这一点?
编辑:
这是我的code:
公开名单<&联络资料GT; readContacts(){
ContentResolver的CR = getContentResolver();
清单<&联络资料GT; CD =新的ArrayList<&联络资料GT;();
光标CUR = cr.query(ContactsContract.Contacts.CONTENT_URI,
NULL,NULL,NULL,ContactsContract.Contacts.DISPLAY_NAME); 如果(cur.getCount()大于0){
而(cur.moveToNext()){
字符串ID = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
字符串名称= cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
如果(的Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))大于0){ //获取电话号码
光标pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,空,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=?,
新的String [] {ID},NULL);
而(pCur.moveToNext()){
字符串手机= pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
如果(!Utils.isEmpty(电话)){
cd.add(新联络资料(身份证,姓名,电话));
}
}
pCur.close();
}
}
}
返回CD;
}
编辑2:
管理,改成只有一个查询来解决它:
公开名单<&联络资料GT; readContacts(){
ContentResolver的CR = getContentResolver();
清单<&联络资料GT; CD =新的ArrayList<&联络资料GT;();
光标pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
新String[]{ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY},
ContactsContract.CommonDataKinds.Phone.NUMBER +IS NOT NULL
空值,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY);
而(pCur.moveToNext()){
字符串手机= pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
字符串ID = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
字符串名称= pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY));
联络资料CD1 = contactDataProvider.get();
cd1.id = ID;
cd1.name =名称;
cd1.phone =电话;
cd.add(CD1);
}
pCur.close();
返回CD;
}
您可以在你的电话号码同时获得姓名和ID:
的String [] =投影新的String [] {Contacts._ID,Contacts.DISPLAY_NAME,Phone.NUMBER};
光标pCur = cr.query(Phone.CONTENT_URI,投影,Phone.CONTACT_ID +=?,
新的String [] {ID},NULL);
这消除了每行子查询。
I am trying to get contacts of the android device. I found a great sample code here
However, when trying to populate a ListActivity
with ArrayAdapter
that is using that code it is taking a lot of time - almost 4 seconds on galaxy s2 and much more on older devices.
I need to improve that performance. What I thought is to implements Cursor
that will hold more than one dimension Cursor
, and use SimpleCursorAdapter
as the list adapter.
I see few problems with this approach:
- I don't know how to get item at specific position.
- each cursor has different columns.
Is there a better/easier way to do that?
EDIT:
here is my code:
public List<ContactData> readContacts(){
ContentResolver cr = getContentResolver();
List<ContactData> cd = new ArrayList<ContactData>();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, ContactsContract.Contacts.DISPLAY_NAME);
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) {
// get the phone number
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));
if (!Utils.isEmpty(phone)) {
cd.add(new ContactData(id, name, phone));
}
}
pCur.close();
}
}
}
return cd;
}
EDIT 2:
managed to fix it by changing it to only one query:
public List<ContactData> readContacts(){
ContentResolver cr = getContentResolver();
List<ContactData> cd = new ArrayList<ContactData>();
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY},
ContactsContract.CommonDataKinds.Phone.NUMBER + " IS NOT NULL",
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY);
while (pCur.moveToNext()) {
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String id = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
String name = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY));
ContactData cd1 = contactDataProvider.get();
cd1.id = id;
cd1.name = name;
cd1.phone = phone;
cd.add(cd1);
}
pCur.close();
return cd;
}
You can get name and ID at the same time you get the phone number:
String[] PROJECTION=new String[] {Contacts._ID, Contacts.DISPLAY_NAME, Phone.NUMBER};
Cursor pCur = cr.query(Phone.CONTENT_URI, PROJECTION, Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
This eliminates the per-row sub-query.
这篇关于得到的接触性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!