0

I am working on an Ecommerce app and need to make filter and populate RecycleView following is my code for RecycleViewAdapter class:

public class RecyclerViewAdapter
        extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> 
        implements Filterable {
    private static final String TAG = "RecyclerViewAdapter";

    private ArrayList<Product> productArrayList;
    final private ArrayList<Product> productArrayListFull;
    private Context mContext;

    public RecyclerViewAdapter(Context context, ArrayList<Product> products){
        this.productArrayList = products;
        this.mContext = context;
        this.productArrayListFull = productArrayList.clone();
    }
} 

productArrayListFull is just assigned and used to get access to all products but never modified productArrayList is modified based on filters

I have refferd to this for creating RecyclerView - Recycler View and this for creating the filters Filter

Whereas the filtering code resets the productArrayListFull to the filtered results

I have changed the function and decided to manually add products using for loop and added logs for size of the ArrayList and in the logs show 0 for ArrayFullList

public class RecyclerViewAdapter
        extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> 
        implements Filterable {
    private static final String TAG = "RecyclerViewAdapter";

    private ArrayList<Product> productArrayList;
    private ArrayList<Product> productArrayListFull;
    private Context mContext;

    public RecyclerViewAdapter(Context context, ArrayList<Product> products){
        this.productArrayList = products;
        this.mContext = context;
        this.productArrayListFull = new ArrayList<>(products);
        for (Product p : products)
        {
            productArrayListFull.add(p);
        }
        Log.v(TAG,"productArrayList Size" + productArrayList.size());
        Log.v(TAG,"productArrayListFull Size" + productArrayListFull.size());
    }
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • You're more likely to get an answer if you include a [mre] in your question, not a link to a picture of some code. – khelwood Feb 16 '20 at 14:32
  • 1
    Please add your code as text, not as a picture. – Daan Seuntjens Feb 16 '20 at 14:34
  • ArrayList.clone() returns Object: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/ArrayList.html#clone() – Johannes Kuhn Feb 16 '20 at 14:45
  • shallow clone of list: https://stackoverflow.com/q/715650/2711811 (elements refer to same object) or deeperclone:https://stackoverflow.com/a/715901/2711811 (elements themselves are reconstructed) –  Feb 16 '20 at 14:58
  • Don't use `clone()`, use `new ArrayList(productArrayList)`. – Mark Rotteveel Feb 16 '20 at 16:47

1 Answers1

0

java.util.ArrayList#clone returns an Object. You will need to cast it to the desired type.

Like so:

this.productArrayListFull = (ArrayList<Product>)productArrayList.clone();

In general I would avoid using .clone() unless you have a compelling reason. Instead write your copying logic yourself. If you want to be sure that productArrayListFull doesn't change, there are a number of immutable collection libraries that can help with this.

PiRocks
  • 1,708
  • 2
  • 18
  • 29