android - How can I make this ContactsContract.Contact query faster with CommonDataKinds.Phone inner query? -
i'm iterating through contacts
table , each iteration iteration on phone
table takes appx. 4 seconds contact list 243 contacts (without iterating phone numbers loads instantly). i'm sure there way make faster.
already made projections fetch needed columns, haven't improved much. maybe should improve sqlite query or iterate in other manner:
contact realmcontact = new contact(); uri uri = contacts.content_uri; string[] projection = { contacts.lookup_key, contacts.display_name_primary, contacts.last_time_contacted, contacts.has_phone_number }; string selection = "((" + commondatakinds.phone.display_name_primary + " notnull) , (" + contacts.has_phone_number + "=1) , (" + commondatakinds.phone.display_name_primary + " != '' ))"; cursor phones = getactivity() .getcontentresolver() .query(uri, projection, selection, null, null); while (phones.movetonext()) { string id = phones.getstring(phones.getcolumnindex(contacts.lookup_key)); string name = phones.getstring(phones.getcolumnindex(contacts.display_name_primary)); string lasttimecontacted = phones.getstring(phones.getcolumnindex(contacts.last_time_contacted)); long data = long.parselong(lasttimecontacted); string date = new simpledateformat("dd/mm/yyyy | hh:mm").format(new date(data)); if (integer.parseint(phones.getstring(phones.getcolumnindex(contacts.has_phone_number))) > 0) { cursor pcur = getactivity().getcontentresolver().query( commondatakinds.phone.content_uri, new string[]{commondatakinds.phone.number}, commondatakinds.phone.lookup_key + " = ?", new string[]{id}, null); int iterationcounter = 0; string phonenumber = ""; while (pcur.movetonext()) { phonenumber += iterationcounter == 0 ? pcur.getstring(pcur.getcolumnindex(commondatakinds.phone.number)) : "," + pcur.getstring(pcur.getcolumnindex(commondatakinds.phone.number)); iterationcounter += 1; } realmcontact.setnumber(phonenumber); log.i("asd-number", phonenumber); pcur.close(); } realmcontact.setid(id); realmcontact.setname(name); realmcontact.setlasttimecontacted( getactivity().getresources() .getstring(r.string.contacts_list_fragment_last_call) + date); realmcontact.setisbeingsaved(true); _realm.insertorupdate(realmcontact);
as @pskink mentioned in comments, need query on phone
table. list of phones contact, can maintain hashmap
contactid
list
of phones.
here's little snippet might (you'd want tweak collection bit store display-name once per contact):
hashmap<long, arraylist<string>> phones = new hashmap<long, arraylist<string>>(); string[] projection = new string[] { phone.contact_id, phone.display_name, phone.number }; cursor c = cr.query(phone.content_uri, projection, null, null, null); while (c != null && c.movetonext()) { long contactid = c.getlong(0); arraylist<string> list = phones.get(contactid); if (list == null) { arraylist<string> list = new arraylist<string>(); phones.put(contactid, list); } list.add(c.getstring(1) + " " + c.getstring(2)); }
Comments
Post a Comment