android 多个cursor 的读取 和读取某个Cursor其他从集合中获取的速度比较

如代码所示,2的速度明显快好几倍

public void getContact(Context con){
Cursor cursor=con.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
PROJECTION,null,null, QUERY_ORDER_BY_NAME);
if (cursor != null) {
try {
List<UContact> uContacts = new ArrayList<UContact>();
if (cursor.moveToFirst()) {
do {
UContact item = new UContact();
item.setId(cursor.getLong(0));
item.setPhotoId(cursor.getLong(1));
item.setName(cursor.getString(2));
item.setFirstChar(getFirstChar(cursor
.getString(android.os.Build.VERSION.SDK_INT >= 8 ? 3
: 2)));
if (android.os.Build.VERSION.SDK_INT >= 8)
item.setSortKey(cursor.getString(3));
queryUMGroupById(con,item, item.getId());
uContacts.add(item);
} while (cursor.moveToNext());
}
Collections.sort(uContacts, new ArrayCompare());
isContactChange = false;
// mContactCount.put(0, uContacts.size());
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
cursor = null;
}
}
}




private void queryUMGroupById(Context con,UContact contact, long id) {
int groupId=0;
Cursor cursor=con.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { CommonDataKinds.GroupMembership.GROUP_ROW_ID }
, Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE + " =?",
new String[] { String.valueOf(id), UM_GROUP_MIMETYPE }, null);

if(cursor!=null){
int count=cursor.getCount();
if(count>0){
cursor.moveToFirst();
groupId=  cursor.getInt(0);
contact.setUmGroupId(groupId);
if(cursor!=null){
cursor.close();
cursor=null;
}

}
}
}


public void getContact2(Context con){
Cursor cursor_group=con.getContentResolver().query(ContactsContract.Data.CONTENT_URI, 
new String[] {Data.CONTACT_ID,CommonDataKinds.GroupMembership.GROUP_ROW_ID }
, Data.MIMETYPE + " =?",
new String[] {UM_GROUP_MIMETYPE }, null);

if(cursor_group!=null){
mGroupMap.clear();
int count=cursor_group.getCount();
if(count>0){
cursor_group.moveToFirst();
for(int i=0;i<count;i++){
int contactId=cursor_group.getInt(0);
int groupId=cursor_group.getInt(1);
mGroupMap.put(contactId, groupId);
cursor_group.moveToNext();
}
if(cursor_group!=null){
cursor_group.close();
cursor_group=null;
}
}
}


Cursor cursor=con.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION,null,null, QUERY_ORDER_BY_NAME);
if (cursor != null) {
try {
List<UContact> uContacts = new ArrayList<UContact>();
if (cursor.moveToFirst()) {
do {
UContact item = new UContact();
item.setId(cursor.getLong(0));
item.setPhotoId(cursor.getLong(1));
item.setName(cursor.getString(2));
item.setFirstChar(getFirstChar(cursor
.getString(android.os.Build.VERSION.SDK_INT >= 8 ? 3
: 2)));
if (android.os.Build.VERSION.SDK_INT >= 8)
item.setSortKey(cursor.getString(3));
item.setUmGroupId(mGroupMap.get(item.getId()));
uContacts.add(item);
} while (cursor.moveToNext());
}
Collections.sort(uContacts, new ArrayCompare());
isContactChange = false;
// mContactCount.put(0, uContacts.size());
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
cursor = null;
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值