简体   繁体   中英

RecyclerView is populating with the last element all my list

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

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)));

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);
            }

notify the adapter

RecyclerAdapter mAdapter = new RecyclerAdapter (R.layout.item_car,mActivity,mCarsArrayList,dialog);
    mRecyclerViewCars.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();

    dialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM