我扩展了AutoCompleteTextView并覆盖Preformfortering功能,以便从数据库中获得结果。我得到了结果,但是什么都没有显示。自定义适配器中的GetView从未被调用。奇怪的是,如果我预紧套件(Inside 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);     
    }


}
有帮助吗?

解决方案

用Mcityautocompleteadapter替换了McityListItems,现在正在工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top