Android edittext - select contact phone number (with autocompletion)

Is there a way to place an EditText element in which I could select a contact phone number from my contact list. Just like in the general application for Android. (Autofill when dialing a phone number or contact name!).

+4
source share
2 answers

You checked the Android Auto Complete example at: http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

perhaps you can customize this example to add your contact list.

+8
source

Hakan's answer is good, but it involves using the cursor. In general, you can write your own adpater, for example, by expanding the ArrayAdapter. In my application, the adapter had to do a lot of “strange things”, such as searching in db, then filter the cursors, add other results, change the others ... I did something like this:

 public class MyAdapter extends ArrayAdapter { private List<String> mObjects; //the "strange Strings" private MyHelper dbHelper; // an helper to make query private MyFilter mFilter; // my personal filter: this is very important!! private final Object mLock=new Object(); //functions very similar to the ArrayAdapter implementation @Override public int getCount() { return mObjects.size(); } @Override public Filter getFilter() { if (mFilter==null) { mFilter=new TeamFilter(); } return mFilter; } @Override public String getItem(int position) { return mObjects.get(position); } @Override public int getPosition(String item) { return mObjects.indexOf(item); } //the trick is here! private class MyFilter extends Filter { //"constraint" is the string written by the user! @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results=new FilterResults(); //no constraint => nothing to return if ((constraint==null)||(constraint.length()==0)) { synchronized (mLock) { ArrayList<String> list=new ArrayList<String>(); results.values=list; results.count=list.size(); } } else { String constr=constraint.toString(); mObjects= // do what you want to do to populate you suggestion - list //( I call the db and change some values) results.values=mObjects; results.count=mObjects.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { if (results.count>0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } } 
+2
source

Source: https://habr.com/ru/post/1335987/


All Articles