如何在Android中扩展Filters时使用publishResults()方法?

我正在处理一个自动完成文本视图,它将closures一个关键值系统,并试图找出我需要做的事情,使publishResults工作,因为结果param传递给publishResults这里在debugging器是正确的,但是我不知道应该对应什么或者如何使它显示结果,任何人都可以帮忙吗? 这个对象的创build在另一个文件中,如下所示:

autoCompleteBox.setAdapter(new AutoCmpAdapter(this, android.R.layout.simple_dropdown_item_1line)); 

其余的代码如下:

 public class AutoCmpAdapter extends ArrayAdapter<String> implements Filterable { protected Filter filter; protected ArrayList<String> items; protected ArrayList<String> res; String lWds[] = { "HOMER", "TOM" }; String sWds[] = { "SIMPSON", "JONES" }; public AutoCmpAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); filter = new PhysFilter(); res = new ArrayList<String>(); } public Filter getFilter() { return filter; } private class PhysFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults f = new FilterResults(); res.clear(); if (constraint != null) { ArrayList<String> res = new ArrayList<String>(); for (int x = 0; x < sWds.length; x++) { if (sWds[x].toUpperCase().startsWith(constraint.toString().toUpperCase())) { res.add(lWds[x]); } } f.values = res.toArray(); f.count = res.size(); } return f; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { if (results.count > 0) { Log.println(Log.INFO, "Results", "FOUND"); notifyDataSetChanged(); } else { Log.println(Log.INFO, "Results", "-"); notifyDataSetInvalidated(); } } } } 

Solutions Collecting From Web of "如何在Android中扩展Filters时使用publishResults()方法?"

首先不要使用String数组。

为关键值对工作,你可以调整你的If语句..尝试在你的onCreate

 AutoCompleteTextView mAutoCompleteTextView; ArrayList<String> lWds = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.testAutoComplete); final AutoCmpAdapter adapter= new AutoCmpAdapter(this, android.R.layout.simple_dropdown_item_1line,lWds); mAutoCompleteTextView.setAdapter(adapter); mAutoCompleteTextView.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { adapter.getFilter().filter(s); } }); } 

和适配器类一样

  public class AutoCmpAdapter extends ArrayAdapter<String> implements Filterable { protected Filter filter; protected ArrayList<String> items; protected ArrayList<String> res; String sWds[] = { "SIMPSON", "JONES" }; public AutoCmpAdapter(Context context, int textViewResourceId,ArrayList<String> listData) { super(context, textViewResourceId,0,listData); filter = new PhysFilter(); res = new ArrayList<String>(); } public Filter getFilter() { return filter; } private class PhysFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults f = new FilterResults(); res.clear(); if (constraint != null) { ArrayList<String> res = new ArrayList<String>(); for (int x = 0; x < sWds.length; x++) { if (sWds[x].toUpperCase().contains(constraint.toString().toUpperCase())) { res.add(sWds[x]); } } f.values = res;//.toArray(); f.count = res.size(); } return f; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { if (results.count > 0) { Log.println(Log.INFO, "Results", "FOUND"); lWds.clear(); lWds.addAll((ArrayList<String>) results.values); notifyDataSetChanged(); } else { Log.println(Log.INFO, "Results", "-"); notifyDataSetInvalidated(); } } } }