我有一个自定义的RecyclerView.Adapter类,我在其中实现了Filterable并使用了接口方法覆盖,如下所示:
private MyFilter myFilter = new MyFilter(); // Class implemented below
并实施了:
@Override
public Filter getFilter() {
return myFilter;
}
这是MyFilter类的实现:
public class MyFilter extends Filter {
@Override
protected Filter.FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResult = new FilterResults();
// If the constraint is 0 return the original data
if(constraint.length() == 0) {
filterResult.values = data;
filterResult.count = data.size();
return filterResult;
}
// Method local properties used to assemble the filtered list
//String folderName;
//String cardsNumber;
//String rowId;
String tempConstraint = constraint.toString().toLowerCase();
// Iterate through the original data array
for (int i = 0; i < data.size(); i++) {
// Get the folder name from the data array
// folderName = data.get(i).familyName;
// cardsNumber = data.get(i).numOfCards;
// rowId = data.get(i).folderRowId;
// Check to see if the constraint variable corresponds to the folder name or to the
// number of cards belonging to this particular folder
if(data.get(i).familyName.toString().toLowerCase().contains(tempConstraint)) {
tempFolderRow = new FolderRow();
tempFolderRow.familyName = data.get(i).familyName;
tempFolderRow.numOfCards = data.get(i).numOfCards;
tempFolderRow.folderRowId = data.get(i).folderRowId;
// Add this object to the updated filtered list
filteredNames.add(data.get(i));
}
}
// Assign the new list to the object filterResult for return
filterResult.values = filteredNames;
filterResult.count = filteredNames.size();
return filterResult;
}
@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
if(results.values != null) {
Log.e("Values: ", results.values.toString());
}
notifyDataSetChanged();
}
}
接下来,在我的活动中,我的editText小部件实现如下:
searchView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
// Filter the folder adapter
mAdapterFolder.getFilter().filter(s);
}
调试我的Filter类(参见上面的代码),当它到达:
时filteredNames.add(data.get(i)); // filteredNames is a List<FolderRow> obj
它返回一个如下所示的异常:
03-08 17:06:36.085 27853-27984 / com.name.cardbox W / Filter:performFiltering()期间发生异常! java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:404) at java.util.AbstractList.add(AbstractList.java:425) 在com.birsan.cardbox.FolderAdapter $ MyFilter.performFiltering(FolderAdapter.java:288) 在android.widget.Filter $ RequestHandler.handleMessage(Filter.java:234) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.os.HandlerThread.run(HandlerThread.java:61)
还有一件事,如果我注释掉发生异常的行,(filteredNames.add(data.get(i));)之前的for循环工作正常,所以我认为这就是问题所在。我究竟做错了什么 ?
答案 0 :(得分:0)
好吧,似乎过滤器不适用于List&lt;&gt;对象。我改变了:
filteredNames
来自
列表&lt; FolderRow&gt;
反对
ArrayList&lt; FolderRow&gt;
。这让它发挥作用。