coders
i face issue that whenever i search in my search menu into recycler view ;It search (filter) but further it newly search from old searched data .
that's a problem
Like if i have data
cipladin
iodex
chupee
paracetamol
then whenever i search in search menu bar "c" then it shows "cipladin" and "chupee" and further when i press "ci" then it shows "cipladin". But when i press backspace so resultnant text will be "c" then at that time it shows only "cipladin" not "chupee" because it search further from old searched data ;not from entire.
My code is
public class itemAdapters extends RecyclerView.Adapter<itemAdapters.MyHolder> implements Filterable {
ArrayList<Items_modal_for_adp> list;
ArrayList<Items_modal_for_adp> listKey;
ArrayList<Items_modal_for_adp> backup;
Context context;
//child thread
public itemAdapters(ArrayList<Items_modal_for_adp> list,ArrayList<Items_modal_for_adp> listKey, Context context) {
this.list =list;
this.listKey =listKey;
backup=list;
this.context = context;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.modal_items,parent,false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
Items_modal_for_adp adp= list.get(position);
Items_modal_for_adp adp2= listKey.get(position);
holder.name.setText(adp.getName());
holder.amount.setText(adp.getAmount());
holder.price.setText("₹ " adp.getPrice());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(context,detail_Item.class);
intent.putExtra("image",adp.getImage());
intent.putExtra("name",adp.getName());
intent.putExtra("description",adp.getDesciption());
intent.putExtra("Amount",adp.getAmount());
intent.putExtra("price",adp.getPrice());
intent.putExtra("uri",adp.getImage());
intent.putExtra("Key",adp2.getKey());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public Filter getFilter() {
return filter;
}
Filter filter=new Filter() {
@Override
protected FilterResults performFiltering(CharSequence keyword) {
ArrayList<Items_modal_for_adp> filtered = new ArrayList<>();
if(keyword.toString().isEmpty())
{
filtered.addAll(backup);
}
else
{
for(Items_modal_for_adp obj : backup)
{
if(obj.getName().toLowerCase().contains(keyword.toString().toLowerCase())) {
filtered.add(obj);
}
}
}
FilterResults results=new FilterResults();
results.values=filtered;
return results;
}
@Override //Main thread
protected void publishResults(CharSequence keyword, FilterResults results) {
list.clear();
list.addAll( (ArrayList<Items_modal_for_adp>) results.values);
//notify from main thread to child thread
notifyDataSetChanged();
}
};
public static class MyHolder extends RecyclerView.ViewHolder
{
TextView name,amount,price;
public MyHolder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.sample_name);
amount=itemView.findViewById(R.id.sample_amount);
price=itemView.findViewById(R.id.sample_price);
}
}
}
So i guess here problem may be "backup" arraylist copy data from updated everytime"list" arraylist .
May be that's a issue .
But i want that backup only copy from old "list" arraylist 1st time and then afterward it has no relationship with "list"
then may be problem can be solved ;but again how to that?
thank you
CodePudding user response:
In ItemAdapters
(class name should be upper case) constructor
backup=list
sets backup
to point to the same object as list
. So backup
is basically an alias for list
.
Later, when
list.clear();
is invoked backup
is cleared too, since list
and backup
point to the same object.
Back in ItemAdapters
constructor, try setting backup
as having it's own copy of the elements, e.g.:
// backup=list
backup = new ArrayList<>(list);
CodePudding user response:
Firstly your class name should start with capital letters i.e. follow the camel case.And, secondly for your filtering you can do something like below:
public class itemAdapters extends RecyclerView.Adapter<itemAdapters.MyHolder> implements Filterable {
private final List<Items_modal_for_adp> list;
private final List<Items_modal_for_adp> itemsModelForAdpSearchList;
public final Context context;
public itemAdapters(Context context, List<Items_modal_for_adp> list) {
this.context = context;
this.list = list;
itemsModelForAdpSearchList = new ArrayList<>(list);
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.activity_demo, parent, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
Items_modal_for_adp adp= list.get(position);
holder.name.setText(adp.getName());
holder.amount.setText(adp.getAmount());
holder.price.setText("₹ " adp.getPrice());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, detail_Item.class);
intent.putExtra("image", adp.getImage());
intent.putExtra("name", adp.getName());
intent.putExtra("description", adp.getDesciption());
intent.putExtra("Amount", adp.getAmount());
intent.putExtra("price", adp.getPrice());
intent.putExtra("uri", adp.getImage());
intent.putExtra("Key", adp2.getKey());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public Filter getFilter() {
return filter;
}
private final Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence keyword) {
ArrayList<Items_modal_for_adp> searchItemAdp = new ArrayList<>();
if (keyword == null || keyword.length() == 0) {
searchItemAdp.addAll(itemsModelForAdpSearchList);
} else {
String filterPattern = keyword.toString().toLowerCase().trim();
for (Items_modal_for_adp obj :itemsModelForAdpSearchList) {
if(obj.getName().contains(filterPattern)) {
searchItemAdp.add(obj);
}
}
}
FilterResults results = new FilterResults();
results.values = searchItemAdp;
return results;
}
@Override //Main thread
protected void publishResults(CharSequence keyword, FilterResults results) {
list.clear();
list.addAll((Collection<? extends Items_modal_for_adp>) results.values);
notifyDataSetChanged();
}
};
public static class MyHolder extends RecyclerView.ViewHolder {
TextView name, amount, price;
public MyHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.sample_name);
amount = itemView.findViewById(R.id.sample_amount);
price = itemView.findViewById(R.id.sample_price);
}
}
}