我试图让我的列表在我从搜索视图中取消搜索时再次显示我的所有项目,但由于某些奇怪的原因,该列表仅停留在上一次搜索的结果中。有谁知道我的代码有什么问题以及如何解决这个问题?我认为与过滤器相关的代码有问题,但我不知道它是什么。
FilterListFragment.java
mpirun -np 2 ./simple_test 1
MST Boost running...
number of mpi processes is 2
number of vertices is 16
number of edges is 16
Constructing graph......completed
printing vertices:
1: (0@1)
1: (1@1)
1: (2@1)
1: (3@1)
1: (4@1)
1: (5@1)
1: (6@1)
1: (7@1)
end of vertex list.
printing edges:
end of edge list.
列出正常状态
以过滤状态列出
答案 0 :(得分:1)
向onQueryTextChange()添加一条日志语句,以便您可以看到过滤器字符串的变化情况。请注意,当搜索视图关闭时,SearchView不会使用空字符串调用onQueryTextChange()。您需要决定何时清除搜索字符串,可能需要添加一个控件,然后自行清除。
答案 1 :(得分:1)
您正在使用原始数据而不是过滤数据。您应该保留对原始数据的引用,并将过滤后的数据用于所有其他目的。这样在清除搜索时就会显示原始数据。
将mData
的所有用法替换为mFilteredData
,如下所示,仅使用原始数据生成过滤后的数据:
private List<String> mData;
private List<String> mFilteredData;
private LayoutInflater mInflater;
private ItemFilter mFilter;
public ItemListAdapter (List<String> data, Context context) {
mData = data;
mFilteredData = data;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mFilteredData.size();
}
@Override
public String getItem(int position) {
return mFilteredData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String strItem = mFilteredData.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_row, parent, false);
holder = new ViewHolder();
holder.mTvItem = (TextView) convertView.findViewById(R.id.tv_item);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTvItem.setText(strItem);
return convertView;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}
/**
* View holder
*/
static class ViewHolder {
private TextView mTvItem;
}
/**
* Filter for filtering list items
*/
private class ItemFilter extends Filter {
/**
* Invoked on a background thread. This is where all the filter logic should go
* @param constraint the constraint to filter on
* @return the resulting list after applying the constraint
*/
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (TextUtils.isEmpty(constraint)) {
results.count = mData.size();
results.values = mData;
} else {
//Create a new list to filter on
List<String> resultList = new ArrayList<>();
for (String str : mData) {
if (str.toLowerCase().contains(constraint.toString().toLowerCase())) {
resultList.add(str);
}
}
results.count = resultList.size();
results.values = resultList;
}
return results;
}
/**
* Runs on ui thread
* @param constraint the constraint used for the result
* @param results the results to display
*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0) {
notifyDataSetInvalidated();
} else {
mFilteredData = (ArrayList<String>)results.values;
notifyDataSetChanged();
}
}
}