开发者

AutoCompleteTextView doesn't show dropdown

I extend AutoCompleteTextView and override preformFiltering function in order to get results from database. I'm getting the results but then nothing is shown. And getView in custom adapter is never called. The strange thing that If I preload items (inside init() function) I can see them... May by anyone can point me to the right solution? Thanks.

public class CityAutoCompleteTextView extends AutoCompleteTextView {
 private DataDatabase mCity;
 private CityAutoCompleteArrayAdapter mCityAutoCompleteAdapter;
    private ArrayList<CityAutoCompleteListItem> mCityListItems;
 public CityAutoCompleteTextView(Context context) { 
  super(context);  
     init();
 }
    public CityAutoCompleteTextView(Context context, AttributeSet attrs) {    
     super(context, attrs);
     init();     
    }

    public CityAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
     super(context, attrs, defStyle);
     init();    开发者_运维知识库 
    }
    public City getItem(int position) {
     return mCityAutoCompleteAdapter.getItem(position).getCity();
    }

    private void init() {
     mCity = new DataDatabase(this.getContext());
     mCityListItems = new ArrayList<CityAutoCompleteListItem>();
     mCityAutoCompleteAdapter = new CityAutoCompleteArrayAdapter(this.getContext(), R.layout.autocomplete_list, mCityListItems);
     this.setAdapter(mCityAutoCompleteAdapter);
    }

    @Override
    protected void performFiltering(CharSequence text, int keyCode) {
     String stext = text.toString();
     Cursor cur = mCity.getCitiesMatches(stext);

     mCityListItems.clear();
     if (cur==null) {
         mCityAutoCompleteAdapter.notifyDataSetChanged();
      return;
     }
     while(!cur.isAfterLast()) {
      City city = new City(cur.getInt(0),cur.getString(1));
      CityAutoCompleteListItem item = new CityAutoCompleteListItem(city, "Unknown province/state",cur.getString(2));
      mCityListItems.add(item);
      cur.moveToNext();      
     };
     cur.close();
     mCityAutoCompleteAdapter.notifyDataSetChanged();

     super.performFiltering(text, keyCode);     
    }


}


Replaced mCityListItems with mCityAutoCompleteAdapter and now it's working.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜