0

I am trying to display contact list in autoCompleteTextView so far I have successfully achieved that but I need to use the contact_id related to name how Should I bind id with name?

I have used hashmap to store the contact list.

here is my code to get contact and add them to hashmap

        Cursor cursor_number=getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
        if(cursor_number!=null){

            if (cursor_number.moveToFirst()){
                do{
                    contact_id=cursor_number.getString(cursor_number.getColumnIndex(ContactsContract.Data._ID));
                    if(Integer.parseInt(cursor_number.getString(cursor_number.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))>0) {
                        Cursor cursor_number1 = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.Data.CONTACT_ID + " = ? ", new String[]{contact_id}, null);
                        if (cursor_number1 != null) {
                            while (cursor_number1.moveToNext()) {
                                String name=cursor_number1.getString(cursor_number1.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                                String id=contact_id;
                                HashMap<String, String> contact_data=new HashMap<String, String>();
                                contact_data.put(id,name);
                            }
                            cursor_number1.close();
                        }
                    }

                }

                while (cursor_number.moveToNext());
                cursor_number.close();
            }
        }

and this is how i'm adding adapter to autoComplete textView

    autoCompleteTextView =(AutoCompleteTextView) this.getActivity().findViewById(R.id.act_network_auto_search);
    autoCompleteTextView.setThreshold(2);
    checkContacts();
    Collection<String> collection=contact_data.values();
    String[] array= collection.toArray(new String[collection.size()]);
    adapter = new ArrayAdapter<String>(getContext(),
            android.R.layout.simple_dropdown_item_1line,array);
    autoCompleteTextView.setAdapter(adapter);

How should I get the id associated with name

any help would be great!

Update :

LinkedHashMap<String, String> contact_data=new LinkedHashMap<String, String>();
contact_data.put(id,name);
rookieDeveloper
  • 2,459
  • 1
  • 22
  • 44

3 Answers3

0

Check this example AutocompleteTextView

Ganesh Pokale
  • 1,538
  • 1
  • 14
  • 28
0

you can get id from contact_data ,if you are sure the names are uniqe

public String getIdByName(String name) {
    for (Entry<String,String> entry : contact_data.entrySet()) {
        if (name.equals(value, entry.getValue())) {
            return entry.getKey();
        }
    }
    return null;
}

check this answer too

Community
  • 1
  • 1
Farid
  • 1,024
  • 9
  • 16
0

If it is compulsary for you to use key valu pair than I will sugest use LinkedHashMap insted of Hasmap because hashmap do not have ordering.

you can put data in linkedhasmap same as hashmap like follows

LinkedHashMap<String,String> lH = new LinkedHashMap<String,String>();
lH.put(id,name);

while in your auto complete text view do folowing

autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View arg1, int pos,
            long id) {
            String key = (new ArrayList<String>(lH.keySet())).get(pos);
            you will have your id in key
       }
});

also you can make above efficient by just initializing lH.keySet out of listner also look at this http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html for LinkedHashMap

Miral Bhalani
  • 274
  • 2
  • 9
  • it returns position I want to access id – rookieDeveloper Oct 19 '16 at 07:17
  • have you tried it ?. i am sure that you will get the id in your key variable in clicklistner. because i am first geting keyset of your linkedhashmap and geting your selected position and then geting key/id at that position that you are selected in your auto completed textview. – Miral Bhalani Oct 19 '16 at 07:35
  • can you please share the code? so i can find problem – Miral Bhalani Oct 19 '16 at 09:04
  • `String key = (new ArrayList(contact_data.keySet())).get(position);` `String selection = (String)parent.getItemAtPosition(position);` I searched for contact name _Warden_ after using debugger it gives this response `1: Warden` – rookieDeveloper Oct 19 '16 at 09:17
  • in debuger what do you have in key variable and what is in selection variable. can you share code where you are seting your linkedlisthashmap? and have you used linkedlisthasmap in this code? – Miral Bhalani Oct 19 '16 at 09:41