Android - Find a contact by display name -
i'm trying find contact display name. goal open contact , add more data (specifically more phone numbers), i'm struggling find contact want update.
this code i'm using:
public static string findcontact(context context) { contentresolver contentresolver = context.getcontentresolver(); uri uri = contactscontract.commondatakinds.phone.content_filter_uri; string[] projection = new string[] { phonelookup._id }; string selection = contactscontract.commondatakinds.phone.display_name + " = ?"; string[] selectionarguments = { "john johnson" }; cursor cursor = contentresolver.query(uri, projection, selection, selectionarguments, null); if (cursor != null) { while (cursor.movetonext()) { return cursor.getstring(0); } } return "john johnson not found"; }
i have contact called "john johnson", method returns "not found". tried searching contact 1 name, makes no difference.
i suspect it's wrong uri, selection or selection arguments, because couldn't find example online of searching contacts given display name, , seems display name special kind of information, different example phone number.
any ideas how can achieve find john johnson?
update: found out how find contact display name:
contentresolver contentresolver = context.getcontentresolver(); uri uri = data.content_uri; string[] projection = new string[] { phonelookup._id }; string selection = structuredname.display_name + " = ?"; string[] selectionarguments = { "john johnson" }; cursor cursor = contentresolver.query(uri, projection, selection, selectionarguments, null); if (cursor != null) { while (cursor.movetonext()) { return cursor.getstring(0); } } return "john johnson not found";
this code returns contact id of first contact display name "john johnson". in original code had wrong uri , wrong selection in query.
i thinks issue may caused projection set. projection used tell android column of data want query give id column display name won't return. try remove projection see whether works.
-- cursor cursor = contentresolver.query(uri, projection, selection, selectionarguments, null);
++ cursor cursor = contentresolver.query(uri, null, selection, selectionarguments, null);
Comments
Post a Comment