1

In my adapter why is .add() working even though I already set the adapter?

mFilePathImages = new ArrayList<>();
mGridAdapter = new HomeGridAdapter(mContext, mFilePathImages);    
mGridRecycler.setLayoutManager(gridLayoutManager);
mGridRecycler.setAdapter(mGridAdapter);

mFilePathImages.add("https://i.redd.it/lawrnev8v8o41.jpg");
mFilePathImages.add("https://i.imgur.com/GQXyo34.jpg");

Shouldn't it only work if I called .notifyDataSetChanged?

I checked my adapter to see if there was .notifyDataSetChanged, but there wasn't.

Here is the the full adapter for the Recycler:

public class HomeGridAdapter extends RecyclerView.Adapter<HomeGridAdapter.ViewHolder> {

    private static final String TAG = "GridViewAdapter";

    private Context mContext;
    private ArrayList<String> mList;


    public HomeGridAdapter(Context context, ArrayList<String> list) {
        this.mContext = context;
        this.mList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.vh_fraghome_griditem, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
        Glide.with(mContext)
                .load(mList.get(i))
                .apply(RequestOptions.centerCropTransform())
                .into(holder.vhSquareImage);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.vh_fraghome_squareimageview)
        SquareImageView vhSquareImage;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
```

Anyone know whats causing the issue

2 Answers2

0

I can assume that this is due to the fact that this code

mFilePathImages.add("https://i.redd.it/lawrnev8v8o41.jpg");
mFilePathImages.add("https://i.imgur.com/GQXyo34.jpg");

executes faster than the process of creating a view for recyclerView starts, so the call to notifyDataSetChanged is not required.Therefore, it works, but not the fact that it will work every time, so you should call notifyDataSetChanged

Destroyer
  • 785
  • 8
  • 20
-1

Java is always pass-by-value. Unfortunately, when we pass the value of an object, we are passing the reference to it, as you did it here :

 new HomeGridAdapter(mContext, mFilePathImages); 

so now, whenever you change your mFilePathImages array, it will also effects the adapter.

For better clarification see this answer : https://stackoverflow.com/a/12429953/9025616

for avoiding this situation you should pass array after you have final data(or any run-time changes). For adapter make list initially empty.

whenever you have your final data send it to your adapter as :

public void updateData(ArrayList<String> list) {
    if (list!= null) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }
}
Nilesh B
  • 937
  • 1
  • 9
  • 14