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);