android - efficient way to load contacts with name and phone and picture with only one query -
i have following code load contacts phones , pictures on samsung s3 device.
public static void getallcontactwithnumberandnameandphoto(context context, arraylist<contactinfo> mcontactlist, boolean starred) { contentresolver cr = context.getcontentresolver(); cursor cur = null; if (starred == true) { cur = cr.query(contactscontract.contacts.content_uri, null, "starred=?", new string[] { "1" }, null); } else { cur = cr.query(contactscontract.contacts.content_uri, null, null, null, null); } if (cur.getcount() > 0) { while (cur.movetonext()) { contactinfo item = new contactinfo(); string id = cur.getstring(cur .getcolumnindex(contactscontract.contacts._id)); string name = cur .getstring(cur .getcolumnindex(contactscontract.contacts.display_name)); // uri photo = phoneutils.getphotourifromid(context, id); string starredvalue = cur.getstring(cur .getcolumnindex(contactscontract.contacts.starred)); boolean isfav = false; if (starredvalue.equals("1")) isfav = true; 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 phoneno = pcur .getstring(pcur .getcolumnindex(contactscontract.commondatakinds.phone.number)); item.addphone(removecharactersfromphonenumber(phoneno)); } pcur.close(); // if (photo != null) { // // item.setphoto(photo.tostring()); // } item.setname(name); item.setfavorite(isfav); item.setrecent(false); mcontactlist.add(item); } } cur.close(); } }
when run code on 1000 contacts takes 40 second load when remove part of loading multiple phones same 1000 contact, takes 1.5 second.
can 1 tell me if there efficient way load contacts phone , not let user wait time.
try this:
private cursor getcontacts() { // run query uri uri = contactscontract.contacts.content_uri; string[] projection = new string[] { contactscontract.contacts._id, contactscontract.contacts.display_name, contactscontract.contacts.photo_uri, contactscontract.contacts.photo_thumbnail_uri, contactscontract.commondatakinds.phone.content_uri }; // put items u need here string selection = contactscontract.contacts.in_visible_group + " = '" + ("1") + "'"; string[] selectionargs = null; string sortorder = contactscontract.contacts.display_name + " collate localized asc"; return getcontentresolver().query(uri, projection, selection, selectionargs, sortorder); }
Comments
Post a Comment