android 多個cursor 的讀取 和讀取某個Cursor其他從集合中擷取的速度比較

來源:互聯網
上載者:User

如代碼所示,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;
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.