I wanted to add a search function to my recyclerview list, I tried the code on the internet by looking at the textbook, it worked fine but if you delete the letters in edittext the list doesn’t go back, it just deletes the list .
Example: If I type ” I 12345 ” in edittext, the rest will be deleted and only ” I 12345 ” will remain, but if I delete the text in edittext, the list will not return.
please help for fix!
here my all codes:
MainActivity java:
public class MainActivity extends AppCompatActivity {
RecyclerViewAdapter recyclerViewAdapter;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview1);
//here start search code
EditText editText = findViewById(R.id.izlash_edittext);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
//here end search code
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
ArrayList<Item> items = new ArrayList<>();
items.add(new Item("start", R.drawable.start));
items.add(new Item("end", R.drawable.end));
recyclerView.setAdapter(recyclerViewAdapter);
}
//here start search code
private void filter(String title) {
ArrayList<Item> filteredList = new ArrayList<>();
for (Item item : recyclerViewAdapter.items) {
if (item.title.toLowerCase().contains(title.toLowerCase())) {
filteredList.add(item);
}
recyclerViewAdapter.filterList(filteredList);
}
}
//here end search code
}
RecyclerViewAdapter java:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
ArrayList<Item> items;
ItemClickListener itemClickListener;
public RecyclerViewAdapter(ArrayList<Item> items, ItemClickListener itemClickListener) {
this.items = items;
this.itemClickListener = itemClickListener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
Item item = items.get(position);
holder.title.setText(item.title);
holder.imageView.setImageResource(item.image);
holder.itemView.setOnClickListener(view -> {
itemClickListener.onClick(item);
});
}
@Override
public int getItemCount() {
return items.size();
}
//here start search code
public void filterList(ArrayList<Item> filteredList) {
items = filteredList;
notifyDataSetChanged();
}
//here end search code
public interface ItemClickListener {
void onClick(Item item);
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView1);
imageView = itemView.findViewById(R.id.imageview1);
}
}
}
Item java:
public class Item {
String title;
int image;
String qisqaMalumot;
String uzunMalumot;
public Item(String title, int image, String qisqaMalumot, String uzunMalumot) {
this.title = title;
this.image = image;
this.qisqaMalumot = qisqaMalumot;
this.uzunMalumot = uzunMalumot;
}
}
please help me.
2
Answers
when you search in
recyclerViewAdapter.items
the recyclerview replaced with new data and old data gone .first make global list
ArrayList<Item> items = new ArrayList<>();
second replace
items
in your for Loop:its done !
First you need to implement the Filterable in your adapter.then you need to keep a filtered item in the listadapter so that you can revert back to its old state. When you login to query from edittext in main activity, this way the adapter can change and revert back to its old state. If you edit the code according to yourself, it will work smoothly.
MainActivity.class..