我的問題是,我已經在我的列表視圖項目上實現了可篩選的搜索視圖。它運作良好,但執行搜索後,searchView
爲空,我無法獲取以前的項目。我嘗試過notifyDataSetChanged
,但它對我無效。我該怎麼辦?刷新列表視圖項目android
....
private ListView mListNotes;
NoteListAdapter na;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListNotes = (ListView) findViewById(R.id.main_listview);
mListNotes.setTextFilterEnabled(true);
}
@Override
protected void onResume() {
super.onResume();
//load data into the listview and display
mListNotes.setAdapter(na);
}
@Override
public boolean onQueryTextChange(String query) {
if (!query.equals("")) {
na.getFilter().filter(query); // filter listView items when text is inputted.
} else {
na.notifyDataSetChanged(); // what do i put here to refresh my list view after search?
}
return false;
}
}
// ListView Adapter
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<>();
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;
}
}
}
你還沒有發佈你的過濾器代碼中的註釋。 –
na.getFilter()。clearFilter();並定義clearFilter() –
clearFilter()包含filterList = originalList和notifyDataSetChanged() –