我需要帮助为当前正在处理的笔记应用中的列表视图项实现可过滤的界面。我尝试了几种方法,但没有得到预期的结果。现在它在绑定搜索项目后什么都不做。 我在我的适配器类和主要活动中实现了所有必要的方法。 我真的需要一些帮助,因为我对此很陌生。感谢期待积极的回复。
// Note.java
public class Note implements Serializable {
private long mDateTime; //creation time of the note
private String mTitle; //title of the note
private String mContent; //content of the note
public Note(long dateInMillis, String title, String content) {
mDateTime = dateInMillis;
mTitle = title;
mContent = content;
}
public void setDateTime(long dateTime) {
mDateTime = dateTime;
}
public void setTitle(String title) {
mTitle = title;
}
public void setContent(String content) {
mContent = content;
}
public long getDateTime() {
return mDateTime;
}
}
public String getTitle() {
return mTitle;
}
public String getContent() {
return mContent;
}
}
// ArrayAdapter which implements Filterable
class NoteListAdapter extends ArrayAdapter<Note> implements Filterable{
List<Note> objects;
private List<Note> mStringFilterList;
Filter filter;
private static final int WRAP_CONTENT_LENGTH = 5;
public NoteListAdapter(Context context, int resource, List<Note> objects) {
super(context, resource, objects);
this.objects = objects;
this.mStringFilterList = objects;
}
@Override
public int getCount() {
return objects.size();
}
@Nullable
@Override
public Note getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Filter getFilter() {
filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<Note> tempList=new ArrayList<Note>();
FilterResults results = new FilterResults();
if (constraint != null && objects != null) {
for(Note singleNote : objects) {
if( singleNote.getTitle().contains(constraint))
tempList.add(singleNote);
}
results.values = tempList;
results.count = tempList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
objects = (ArrayList<Note>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.list_component, parent, false);
}
return convertView;
}
}
// Main Activity
@Override
public boolean onQueryTextSubmit(String query) {
ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext());
final NoteListAdapter na = new NoteListAdapter(this, R.layout.list_component, notes);
na.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
答案 0 :(得分:0)
您将返回super.getFilter()而不是刚刚创建的过滤器。而且,如果performFiltering()
有某些内容并且适配器包含项目,那么您在过滤器CharSequence
方法中没有过滤任何内容,因为您要将每个项目添加到结果中。
我会建议这样的事情:
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<Note> tempList=new ArrayList<Note>();
FilterResults results = new FilterResults();
if (constraint != null && objects != null) {
for(Note singleNote : objects) {
if( singleNote.getTitle().contains(constraint)
tempList.add(singleNote);
}
results.values = tempList;
results.count = tempList.size();
}
return results;
}
<强>编辑:强>
此外,在活动中,
内@Override
public boolean onQueryTextSubmit(String query) {
ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext());
final NoteListAdapter na = new NoteListAdapter(this, R.layout.list_component, notes);
na.getFilter().filter(query);
return false;
}
您正在使用新的适配器而不将其设置为recyclerView / listView。
尝试使用您在活动的onCreate()
内创建的适配器,或在使用recyclerView/listView
setAdapter(na)
。na.getFilter().filter(query);
<强> EDITS2:强>
因此,如果您希望在用户输入内容时执行过滤器,请将代码从onQueryTextSubmit()
移至onQueryTextChanged()
。
对于你要求的改进(当字符串为空时重新加载完整列表)你有两种方法:
第一种方式:
@Override
public boolean onQueryTextChanged(String query) {
if (!query.equals("")) {
ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext());
final NoteListAdapter na = new NoteListAdapter(this, R.layout.list_component, notes);
na.getFilter().filter(query);
} else {
ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext());
final NoteListAdapter na = new NoteListAdapter(this, R.layout.list_component, notes);
}
return false;
}
另一种方式可能在Filter接口实现的performFiltering()
方法内。您还可以检查字符串是否为空,如果是,则需要返回完整列表,而不创建新的支持,只有在某些条件匹配时才添加元素。