Android AutoCompleteTextView不';不显示下拉列表
为了从数据库中获取结果,我扩展了AutoCompleteTextView并覆盖了preformFiltering函数。 我得到了结果,但是什么都没有显示。并且从未调用自定义适配器中的getView。 奇怪的是,如果我预加载项(在init()函数中),我可以看到它们。。。 谁能给我指出正确的解决办法? 谢谢Android AutoCompleteTextView不';不显示下拉列表,android,autocompletetextview,Android,Autocompletetextview,为了从数据库中获取结果,我扩展了AutoCompleteTextView并覆盖了preformFiltering函数。 我得到了结果,但是什么都没有显示。并且从未调用自定义适配器中的getView。 奇怪的是,如果我预加载项(在init()函数中),我可以看到它们。。。 谁能给我指出正确的解决办法? 谢谢 公共类CityAutoCompleteTextView扩展了AutoCompleteTextView{ 私有数据库mCity; 私人城市自动完成适配器MCITY自动完成适配器; 私人Array
公共类CityAutoCompleteTextView扩展了AutoCompleteTextView{
私有数据库mCity;
私人城市自动完成适配器MCITY自动完成适配器;
私人ArrayList mCityListItems;
公共城市自动完成文本视图(上下文){
超级(上下文);
init();
}
public CityAutoCompleteTextView(上下文上下文,属性集属性){
超级(上下文,attrs);
init();
}
public CityAutoCompleteTextView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
init();
}
公共城市getItem(内部位置){
返回mcityautompleteadapter.getItem(position.getCity();
}
私有void init(){
mCity=newdatabase(this.getContext());
mCityListItems=newarraylist();
mcityautompleteadapter=new cityaoutompletearrayadapter(this.getContext(),R.layout.autocomplete_list,mCityListItems);
此.setAdapter(mcityautompleteadapter);
}
@凌驾
受保护的void性能过滤(字符序列文本,int-keyCode){
字符串stext=text.toString();
游标cur=mCity.getCitiesMatches(stext);
mCityListItems.clear();
如果(cur==null){
mcityautompleteadapter.notifyDataSetChanged();
返回;
}
而(!cur.isAfterLast()){
城市=新城(cur.getInt(0),cur.getString(1));
CityAutoCompleteListItem=new CityAutoCompleteListItem(城市,“未知省/州”,cur.getString(2));
mCityListItems.添加(项目);
cur.moveToNext();
};
cur.close();
mcityautompleteadapter.notifyDataSetChanged();
超级。性能过滤(文本、键码);
}
}
用mCityAutoCompleteAdapter替换了mCityListItems,现在它可以工作了
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);
}
}