我已经编辑了Adapter类并扩展了filterable类。我的搜索功能有效,但是我无法获得当前列表。
这是我的适配器类:
public class ListViewAdapter extends BaseAdapter implements Filterable{
Context context;
private List<Student> studentList;
private List<Student> orginStudentList;
public ListViewAdapter(Context context,List<Student> studentList)
{
this.studentList = new ArrayList<>();
this.studentList = studentList;
this.orginStudentList = studentList;
this.context = context;
}
@Override
public Filter getFilter() {
if(nameFilter == null)
nameFilter = new NameFilter();
return nameFilter;
}
public void resetData() {
studentList = orginStudentList;
}
private class NameFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if ((constraint == null) || (constraint.length() == 0)) {
// No filter implemented we return all the list
results.values = orginStudentList;
results.count = orginStudentList.size();
}else if (Character.isDigit(constraint.charAt(constraint.length()-1))){
results.values = studentList;
results.count = studentList.size();
}
else {
// We perform filtering operation
List<Student> nStudentList = new ArrayList<Student>();
for (Student stu : orginStudentList) {
String name = stu.getName();
if (name.contains(constraint))
nStudentList.add(stu);
}
results.values = nStudentList;
results.count = nStudentList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
studentList = (List<Student>)results.values;
notifyDataSetChanged();
}
}
} 我会检查结果是否为零或为空,是否返回当前列表?我想念什么?
答案 0 :(得分:0)
添加notifyDataSetChanged();内部适配器
答案 1 :(得分:0)
private void filter(String text) {
List<Model> filterdNames = new ArrayList<>();
for (Model s : Models) {
if (s.getName().contains(text)) {
filterdNames.add(s);
}
}
//calling a method of the adapter class and passing the filtered list
recyclerAdapter.filterList(filterdNames);
}