Implementing Search Filter Causing Problem When Pressed Backspace
I am trying to implement a search filter on complex object data in recyclerview when I type in the Edit text first-time it works properly, but whenever I try to delete characters b
Solution 1:
I assume the arrcopy is the copy of arr like:
ArrayList<User> arrcopy = new ArrayList<>(arr);
If you modify arr, it also change the content of arrcopy. And when no result matches, the arr is empty, so is the arrcopy.
else {
temp.addAll(arrcopy);
}
arr.clear();
arr.addAll(temp);
Now temp is empty, the arr data you set for adapter is empty, so issues happens. Please try: In MyAdapter:
Context cxt;
ArrayList<user> arr;
publicMyAdapter(Context context){
cxt = context;
dref = FirebaseDatabase.getInstance().getReference("users");
}
publicvoidsetData(ArrayList<User> arrayList){
arr = arrayList;
notifyDataSetChanged();
}
Also put the filter() in your activity/fragment:
protectedvoidonCreate(final Bundle savedInstanceState) {
etSearch = (EditText) findViewById(R.id.edittext);
etSearch.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
etSearch.requestFocus();
}
});
etSearch.addTextChangedListener(newTextWatcher() {
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
}
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
filter(etSearch.getText().toString());
}
@OverridepublicvoidafterTextChanged(Editable s) {
}
});
etSearch.clearFocus();
mAdapter = newMyAdapter(this);
mAdapter.setData(arrayList);
mRecyclerView.setAdapter(mAdapter);
...
}
publicvoidfilter(String input) {
if (!TextUtils.isEmpty(input)) {
ArrayList<User> temp = newArrayList<>();
for (user s : arr) {
if (s.getName().toLowerCase().contains(input)) {
temp.add(s);
}
}
mAdapter.setData(temp);
} else {
mAdapter.setData(arr);
}
mAdapter.notifyDataSetChanged();
}
Solution 2:
Implement Filterable
in your MyAdapter
publicclassMyAdapterextendsRecyclerView.Adapter<MyAdapter.MyHolder>
implementsFilterable {
// change MyObject to user class//replace myObjects with arr and reservedList with arrcopy
Context cxt;
ArrayList<MyObject> myObjects;
ArrayList<MyObject> reservedList;
DatabaseReference dref;
publicMyAdapter(Context context, ArrayList<MyObject> arrayList){
cxt = context;
myObjects = arrayList;
reservedList = arrayList
Log.d("Very start arr",myObjects.toString());
// notifyDataSetChanged();
dref = FirebaseDatabase.getInstance().getReference("users");
}
//...@Overridepublic Filter getFilter() {
returnnewFilter() {
@Overrideprotected FilterResults performFiltering(CharSequence charSequence) {
StringcharString= charSequence.toString().toLowerCase();
if (!charString.isEmpty()) {
List<MyObject> filteredList = newArrayList<>();
for (MyObject row : reservedList) {
if (row.getName().toLowerCase().contains(charString)){
filteredList.add(row);
}
}
myObjects = filteredList;
} else {
myObjects = reservedList;
}
FilterResultsfilterResults=newFilterResults();
filterResults.values = myObjects;
return filterResults;
}
@OverrideprotectedvoidpublishResults(CharSequence charSequence, FilterResults filterResults) {
myObjects = (ArrayList<MyObject>) filterResults.values;
notifyDataSetChanged();
}
};
}
and from your activity
etSearch = (EditText) findViewById(R.id.edittext);
etSearch.addTextChangedListener(newTextWatcher() {
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
}
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
myAdapter.getFilter().filter(s);
}
@OverridepublicvoidafterTextChanged(Editable s) {
}
});
Post a Comment for "Implementing Search Filter Causing Problem When Pressed Backspace"