我已經實現Filterable
我AutoCompleteTextView
:機器人:沒有什麼自動完成結果顯示出來
public class IdiomAdapter extends BaseAdapter implements Filterable{
Context context;
List<Idiom> idioms;
public IdiomAdapter(Context context, List<Idiom> idioms){
this.context = context;
this.idioms = idioms;
originaList = new ArrayList<Idiom>(idioms);
filter = new IdiomFilter();
}
@Override
public int getCount() {
if (idioms != null) {
return idioms.size();
}else{
return 0;
}
}
@Override
public Object getItem(int arg0) {
return idioms.get(arg0);
}
@Override
public long getItemId(int arg0) {
return idioms.get(arg0).getId();
}
@Override
public Filter getFilter() {
return filter;
}
private IdiomFilter filter;
ArrayList<Idiom> originaList;
public class IdiomFilter extends Filter {
public IdiomFilter(){
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults oReturn = new FilterResults();
ArrayList<Idiom> result = new ArrayList<Idiom>();
if (originaList == null) {
originaList = (ArrayList<Idiom>) idioms;
}
if (constraint !=null) {
if (originaList !=null && originaList.size() > 0) {
for (Idiom idiom : originaList) {
if (idiom.getVerb().contains(constraint))
result.add(idiom);
}
}
oReturn.values = result;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
idioms = (ArrayList<Idiom>)results.values;
notifyDataSetChanged();
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.simple_list_item_1, null);
}
return v;
}
而且在我的活動使用它:
IdiomAdapter adapter = new IdiomAdapter(MainActivity.this, idioms);
AutoCompleteTextView txtSearch = (AutoCompleteTextView) findViewById(R.id.txtSearch);
txtSearch.setAdapter(adapter);
當我
AutoCompleteTextView
關鍵字輸入
,似乎工作但沒有結果顯示:
任何想法?
檢查數組列表是有值或不???? – 2014-09-22 05:24:52
是的,值被填充。 – 2014-09-22 05:27:41