android - showing contacts by filtering account_type column from RawContacts -
im getting showing contacts on recyclerview
, below code retrieve contacts
uri contact_uri=contactscontract.commondatakinds.phone.content_uri; return new cursorloader(getactivity(),contact_uri,null,null,null,build.version.sdk_int >= build.version_codes.honeycomb ? contactscontract.contacts.display_name_primary : contactscontract.contacts.display_name+ "asc");
but contacts being shown multiple times decided filter contacts on account_type
column. below code filter based on account_type
if(list.getstring(list.getcolumnindex("account_type")).equals("local phone account") || list.getstring(list.getcolumnindex("account_type")).equals("sim account") ) { textview.settext(list.getstring(list.getcolumnindex(build.version.sdk_int >= build.version_codes.honeycomb ? contactscontract.contacts.display_name_primary : contactscontract.contacts.display_name))); number.settext(list.getstring(list.getcolumnindex(contactscontract.commondatakinds.phone.number))); number.settext(list.getstring(list.getcolumnindex("account_type"))); }
problem values of account_type
sim , phone contacts varies device device. in samsung gt-l9082
gives values sim "sim account" , phone "local phone account" when tested on galaxy j5
shows different values against account_type
sim , phone contacts.i want show sim , phone contacts
this not way go.
commondatakinds.phone.content_uri
table of all phones in contacts db, not contacts. if filter 1 account, if contact contains more 1 phone, it'll appear twice in list.
if want display 1 row per contact, still need display phone in main list, can't use cursorloader
paradigm (which sucks , wouldn't use anyway).
instead run simple query items in phones.content_uri
table, , create hashmap
contact_id
list of number
s, , display 1 row per item in map, , you'll access contact's list of phones display.
map<string, list<string>> contacts = new hashmap<string, list<string>>(); string[] projection = { phone.contact_id, phone.display_name, phone.number }; cursor cur = cr.query(phone.content_uri, projection, null, null, null); while (cur != null && cur.movetonext()) { long id = cur.getlong(0); string name = cur.getstring(1); string data = cur.getstring(2); // actual info, e.g. +1-212-555-1234 log.d(tag, "got " + id + ", " + name + ", " + data); // add info existing list if contact-id found, or create new list in case it's new string key = id + " - " + name; list<string> infos; if (contacts.containskey(key)) { infos = contacts.get(key); } else { infos = new arraylist<string>(); contacts.put(key, infos); } infos.add(data); } // can iterate on 'contacts' map display contacts
Comments
Post a Comment