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(); } } } }
Paolo source share