Reputation: 179
I have a RecyclerView that inflates 5 images, I get the images and loop through each one , then place it into the model and show it inside a dialog, but the thing is that is showing the same 5 images instead of showing different ones.
public void showHeartDialog() {
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.dialog_most_used_cars);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
mRecyclerViewFrases = dialog.findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
mRecyclerViewFrases.setLayoutManager(linearLayoutManager);
for (int i = 0; i < mDataOfUse.getMostUsedCars.size(); i++) {
model.setImage(mGetBitmap.getBitmapOfCar(mDataOfUse.getOrderedCars().get(i)));
mCarsArrayList.add(model);
}
RecyclerAdapter mAdapter = new RecyclerAdapter (R.layout.item_car,mActivity,mCarsArrayList,dialog);
mRecyclerViewCars.setAdapter(mAdapter);
dialog.show();
}
My adapter bind viewholder
@Override
public void onBindViewHolder(FavoriteCarsViewHolder holder, int position) {
CarModel model = mCarArrayList.get(position);
holder.carFav.setImageBitmap(model.getImage());
}
And my model
public class CarModel{
private Bitmap image;
public CarModel() {
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
}
And the problem is that my recyclerview is showing 5 cars, but it seems is the last element of the arraylist of cars, its populating with the same car the 5 spaces when it should show the different 5 cars loaded into the arraylist
thanks
Upvotes: 0
Views: 169
Reputation: 1491
notify the adapter
RecyclerAdapter mAdapter = new RecyclerAdapter (R.layout.item_car,mActivity,mCarsArrayList,dialog);
mRecyclerViewCars.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
dialog.show();
Upvotes: 0
Reputation: 376
add
CarModel model = new CarModel(); in for loop
for (int i = 0; i < mDataOfUse.getMostUsedCars.size(); i++) {
CarModel model = new CarModel();
model.setImage(mGetBitmap.getBitmapOfCar(mDataOfUse.getOrderedCars().get(i)));
mCarsArrayList.add(model);
}
Upvotes: 1
Reputation: 671
I think that's because your whole mCarsArrayList
is refrerring to the same object.
Try Adding this line
model = new Model();
before
model.setImage(mGetBitmap.getBitmapOfCar(mDataOfUse.getOrderedCars().get(i)));
Upvotes: 1