Hunt
Hunt

Reputation: 8435

filter contact names and remove email addresses

I am using Filter with ListView which is populated trough Contact data which contains Names and Number.

Now i got two problems when i type a text into EditText which in turns fires adapter.getFilter().filter(s.toString())

1) When i type 'aa' latter (in my code )

i can see name starting from 'aa' for example aakruti , but at the same time i am able to view email addresses too , which i don't wanted to make it visible when a filter is fired.

enter image description here

2) When i type 'aa' latter (in phone's inbuilt contact list)

i can see name starting from 'aa' for example aakruti

but i am missing one name i.e. S A T Y A ( which is shown by contact search when i type 'aa' latter into it )

enter image description here

here is my filter query , inside runQueryOnBackgroundThread

StringBuilder buffer = null;
String[] args = null;
    if (constraint != null) {
    buffer = new StringBuilder();
    buffer.append("UPPER(");
    buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
    buffer.append(") GLOB ?");
    args = new String[] { constraint.toString().toUpperCase() + "*" };
    }
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
System.out.println(buffer);
return mContent.query(
            ContactsContract.Contacts.CONTENT_URI, 
            projection,
            buffer == null ? null : buffer.toString(), 
            args,sortOrder
);

projection data

   public static String[] projection = new String[] {
           ContactsContract.Contacts._ID,
           ContactsContract.Contacts.DISPLAY_NAME

   };

EDIT

So far i tried to access

ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME

But GIVEN_NAME displays few email address too and even it shows contact name which has email address

for example ,

[email protected] 
Raul Jakson          (which only has email address no contact number)
Raul Jakson          ( i see this name twice as it has two different email address , but i wanna see it as name )

so can anyone tell me how can i limit the email addresses and show only NAMES which has only contact phone numbers ?

Upvotes: 0

Views: 1466

Answers (1)

SoftWyer
SoftWyer

Reputation: 2207

Firstly, the emails show up because that contact has no name. In these cases, Android will use the email as the display name. To avoid showing these, use GIVEN_NAME and FAMILY_NAME. See the api docs for StructuredName.

Secondly, you don't find SATIYA as your query is looking for a DISPLAY_NAME that begins with AA. SATIYA is not a word, it's a sequence of initials, e.g. S A T I Y A. If you want to find these, then you'll have to craft your query to cater for this. You could search for *A*A*, but you'll probably get many others hits too. I suspect Android is doing some variant of an initial+surname search where BO would find Barack Obama.

Upvotes: 2

Related Questions