2

我扩展 AutoCompleteTextView 并覆盖 preformFiltering 函数以便从数据库中获取结果。我得到了结果,但没有显示任何内容。并且永远不会调用自定义适配器中的 getView 。奇怪的是,如果我预加载项目(在 init() 函数中)我可以看到它们......可能有人能指出我正确的解决方案吗?谢谢。

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);     
    }


}
4

1 回答 1

2

用 mCityAutoCompleteAdapter 替换了 mCityListItems,现在它可以工作了。

于 2010-08-13T21:02:49.883 回答