我有一个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();
}
我的适配器绑定视图持有人
@Override
public void onBindViewHolder(FavoriteCarsViewHolder holder, int position) {
CarModel model = mCarArrayList.get(position);
holder.carFav.setImageBitmap(model.getImage());
}
还有我的模特
public class CarModel{
private Bitmap image;
public CarModel() {
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
}
问题是我的recyclerview显示了5辆车,但似乎是汽车arraylist的最后一个元素,当它应该显示加载到arraylist中的5辆不同的5辆车时,它在同一辆车中填充了5个空格< / p>
谢谢
答案 0 :(得分:1)
我认为这是因为您的整个mCarsArrayList
都指向同一对象。
尝试添加此行
model = new Model();
之前
model.setImage(mGetBitmap.getBitmapOfCar(mDataOfUse.getOrderedCars().get(i)));
答案 1 :(得分:1)
添加
CarModel model = new CarModel();在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);
}
答案 2 :(得分:0)
通知适配器
RecyclerAdapter mAdapter = new RecyclerAdapter (R.layout.item_car,mActivity,mCarsArrayList,dialog);
mRecyclerViewCars.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
dialog.show();