简体   繁体   English

RecyclerView正在使用我所有列表中的最后一个元素进行填充

[英]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. 我有一个RecyclerView,可以放大5张图像,获取每张图像并循环遍历,然后将其放入模型中并在对话框中显示,但事实是显示的是5张相同的图像,而不是显示不同的图像。

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 问题是我的recyclerview显示了5辆车,但这似乎是汽车arraylist的最后一个元素,当同一辆车填充5个空格时,它应该显示加载到arraylist中的5辆不同

thanks 谢谢

I think that's because your whole mCarsArrayList is refrerring to the same object. 我认为这是因为整个mCarsArrayList都引用同一对象。

Try Adding this line 尝试添加此行

model = new Model();

before 之前

model.setImage(mGetBitmap.getBitmapOfCar(mDataOfUse.getOrderedCars().get(i)));

add

CarModel model = new CarModel(); CarModel模型= new CarModel(); in for loop 在for循环中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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