有没有办法放置EditText
元素,我可以从联系人列表中选择联系人的电话号码。就像在通用的android短信应用程序中一样。 (键入电话号码或联系人姓名时自动完成!)。
答案 0 :(得分:8)
您是否在http://developer.android.com/resources/tutorials/views/hello-autocomplete.html
检查了Android的自动完成示例也许您可以调整此示例来添加联系人列表。
答案 1 :(得分:2)
Hakan的答案很好,但它与Cursor的使用有关。 通常,您可以编写自己的adpater,例如通过扩展ArrayAdapter。 在我的应用程序中,适配器必须做很多“奇怪的事情”,比如在db中搜索,然后重新过滤游标,添加其他结果,更改其他... 我做了这样的事情:
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();
}
}
}
}