Android: How to get contact DisplayName by contact _ID? -
folks!
look broke mind problem - how display_name contactcontract via _id got earlier???
here code:
public static long getid(context context, string number) { long id = 0; string displayname; // define columns want query return final string[] projection = new string[] { contactscontract.phonelookup.display_name, contactscontract.phonelookup._id}; // encode phone number , build filter uri final uri contacturi = uri.withappendedpath(contactscontract.phonelookup.content_filter_uri, uri.encode(number)); // query time final cursor cursor = context.getcontentresolver().query(contacturi, projection, null, null, null); if(cursor != null) { if (cursor.movetofirst()) { displayname = cursor.getstring(cursor.getcolumnindex(contactscontract.phonelookup.display_name)); // here _id! id = cursor.getlong(cursor.getcolumnindex(contactscontract.phonelookup._id)); log.d("_tag_", displayname); { // // v. n-1 // cursor cursor2 = context.getcontentresolver().query( contacturi, projection, contactscontract.commondatakinds.phone.contact_id + " = ?", new string[]{string.valueof(id)}, null); if(cursor2 != null) { // cursor valid string below got error "android.database.cursorindexoutofboundsexception: index -1 requested, size of 1 : )" displayname = cursor2.getstring(cursor.getcolumnindex(contactscontract.phonelookup.display_name)); log.d("_tag_", displayname); } } { // // v. n // uri myphoneuri = uri.withappendedpath(contactscontract.phonelookup.content_filter_uri, uri.encode(string.valueof(id))); cursor phonecursor = context.getcontentresolver().query( myphoneuri, null, null, null, null); (phonecursor.movetofirst(); !phonecursor.isafterlast(); phonecursor.movetonext()) { // no steps inside! string str = phonecursor.getstring(phonecursor .getcolumnindexorthrow(contactscontract.commondatakinds.nickname.name)); log.d(app.log_tag, "contactutils.getid: " + str); } if(phonecursor != null) { phonecursor.close(); } } } else { log.d("_tag_", "contact not found @ " + number); } cursor.close(); } return id; } "v.n-1" , "v.n" 2 ones million attempts before.
see - got _id, why doesn't work valid query???
here decision found.
public static string getdisplayname(context context, long id) { string displayname = null; // define columns want query return final string[] projection = new string[] { contactscontract.contacts.display_name, }; final cursor cursor = context.getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, projection, contactscontract.commondatakinds.phone.contact_id + " = ?", new string[] { id + "" }, null); if(cursor != null) { if (cursor.movetofirst()) { displayname = cursor.getstring(cursor.getcolumnindex(contactscontract.contacts.display_name)); } cursor.close(); } return displayname; } it works...
Comments
Post a Comment