我扩展了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);
}
}
答案 0 :(得分:2)
用mCityAutoCompleteAdapter替换了mCityListItems,现在它正在运行。