开发者

Android edittext - choose contact's phone number (with auto completion)

Is th开发者_Python百科ere a way to put a EditText element in which i could choose contact's phone number from my contacts list. Just as in generic android sms app. (Auto-complete when typing phone number or contact name!).


Have you checked out Android's Auto Complete example at : http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

maybe you can tweak this example to add your contact list.


Hakan's answer is good, but it's related to Cursor use. In general you can write your own adpater, for instance by extending the ArrayAdapter. In my application, the Adapter had to do a lot of "strange things" like search in db, then re-filter the Cursors, add other results, changing other... 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();
    }
}
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜