My problem is that I have implemented search view with filterable on my list view items. It works well but after a search is performed and the searchView
is empty, I can't get the previous items. I have tried notifyDataSetChanged
but it did not work for me. What do I do?
....
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;
}
}
}
You are replacing original data with your filtered data. Use the below code:
class NoteListAdapter extends ArrayAdapter<Note> implements Filterable {
List<Note> objects;
private List<Note> originalList = new ArrayList<>();
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.originalList.addAll(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 (objects != null) {
if(constraint != null && !constraint.equals("")) {
for (Note singleNote : objects) {
if (singleNote.getTitle().contains(constraint))
tempList.add(singleNote);
}
results.values = tempList;
results.count = tempList.size();
}else {
tempList.clear();
tempList.addAll(originalList);
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;
}
}
Hope it helps:)
Read comments where code needs to change or replace whole adapter class with this class
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 mStringFilterList.size(); // change this line
}
@Nullable
@Override
public Note getItem(int position) {
return mStringFilterList.get(position); // change this line
}
@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 && constraint.length() > 0 && objects != null) {
for(Note singleNote : objects) {
if( singleNote.getTitle().contains(constraint))
tempList.add(singleNote);
}
results.values = tempList;
results.count = tempList.size();
}
// add else block.
else
{
results.values = objects;
results.count = objects.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mStringFilterList = (ArrayList<Note>) results.values; // change this line
notifyDataSetChanged();
}
};
return filter;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.