我无法让我的ListView进行排序。当我输入SearchView时,没有任何反应。我的ListView没有任何变化。我一直在比较这里和其他地方的例子,没有运气。我已通过调试确认过滤器实际上有效,我可以看到filteredArray填充了过滤数据,但同样没有ListView更改。
这是我到目前为止所拥有的。在MainActivity.class ..
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSubmitButtonEnabled(false);
searchView.setQueryHint("Enter #..");
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
ListView listView = (ListView) findViewById(R.id.main_listView);
listView.setTextFilterEnabled(true);
if (s.isEmpty()) {
listView.clearTextFilter();
} else {
customAdapter.getFilter().filter(s);
}
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
我像这样创建customAdapter对象,listCustom包含所有CustomResults类。
customAdapter = new CustomAdapter(MainActivity.this, listCustom);
CustomAdapter:
public class CustomAdapter extends BaseAdapter implements Filterable {
private static ArrayList<CustomResults> customArrayList;
private static ArrayList<CustomResults> filteredArrayList;
private LayoutInflater mInflater;
public CustomAdapter(Context context, ArrayList<CustomResults> results) {
customArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return customArrayList.size();
}
public Object getItem(int position) {
return customArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.num = (TextView) convertView.findViewById(R.id.number);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(customArrayList.get(position).getName());
holder.num.setText(customArrayList.get(position).getNum());
return convertView;
}
static class ViewHolder {
TextView name;
TextView num;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0) {
notifyDataSetInvalidated();
} else {
filteredArrayList = (ArrayList<CustomResults>) results.values;
notifyDataSetChanged();
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
int count = customArrayList.size();
ArrayList<CustomResults> filteredList = new ArrayList<CustomResults>();
for (CustomResults custom : customArrayList) {
filteredList.add(custom);
}
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = customArrayList.get(i).getNum();
if (filterableString.toLowerCase().contains(filterString)) {
filteredList.add(customArrayList.get(i));
} else if (customArrayList.get(i).getName().toLowerCase().contains(filterString)) {
filteredList.add(customArrayList.get(i));
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
};
return filter;
}
答案 0 :(得分:0)
getView()和其他函数总是从customArrayList检索,而不是从filteredArrayList检索。