所以我希望将图像添加到我的物品中。现在,我可以在RecyclerView中为每个CardView显示单词。但是,对于图片,我无法这样做。我尝试添加数据类型为ImageView / Drawable的listImage <>,但是它仍然不起作用(尽管它适用于listData <>,但其数据类型为字符串)。 这是我的RecyclerAdapter中的代码:
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
public TextView txtDesc;
public ImageView imgView;
private ItemClickListener itemClickListener;
public RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
txtDesc = (TextView)itemView.findViewById(R.id.foodcourtdesc);
imgView = (ImageView)itemView.findViewById(R.id.foodcourtimg);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder>{
private List<String> listData = new ArrayList<>();
private Context context;
public RecyclerAdapter(List<String> listData, Context context) {
this.listData = listData;
this.context = context;
}
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.foodcourt, parent, false);
return new RecyclerViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
holder.txtDesc.setText(listData.get(position));
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
if(isLongClick){
Toast.makeText(context, listData.get(position) + " chosen!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, listData.get(position) + " chosen!", Toast.LENGTH_SHORT).show();
}
}
});
}
这是我插入数据时的代码
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_foodcourt);
recyclerView.setHasFixedSize(true);
layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerAdapter(listData, this);
recyclerView.setAdapter(adapter);
for (int i = 1; i <= 6; i++){
listData.add("Foodcourt " + i);
}
我使用了video中的代码来构成此RecyclerView / CardView,但是即使了解了它的工作原理(部分),我仍然无法为每一项分配照片。请帮助我,谢谢!
答案 0 :(得分:1)
您的循环代码不在正确的位置。您将数据传递到构造函数后,将其添加到列表中。这是修复它的代码:
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_foodcourt);
recyclerView.setHasFixedSize(true);
layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
for (int i = 1; i <= 6; i++){
listData.add("Foodcourt " + i);
}
adapter = new RecyclerAdapter(listData, this);
recyclerView.setAdapter(adapter);
答案 1 :(得分:0)
listData
应该包含images
个不同的URLs
,以便在images
中的不同卡上显示不同的listView
。
答案 2 :(得分:0)
如果图像来自可绘制图像,则编写:
Integer[] images = new Integer[]{R.drawable.image1, R.drawable.image2....}; // Provide 6 Images or it will crash saying null pointer exception
ArrayList<Integer> loadImage = new ArrayList<>();
在您的活动课中。
然后,在您的for循环中编写:
for (int i = 1; i <= 6; i++){
listData.add("Foodcourt " + i);
loadImage.add(images[i]);
}
然后进行更改
adapter = new RecyclerAdapter(listData, loadImage, this);
recyclerView.setAdapter(adapter);
然后,在您的适配器中将Arraylist设置为:
ArrayList<Integer> Images = new ArrayList<>();
然后,按照以下代码进行修改:
public RecyclerAdapter(List<String> listData, List<Integer> listImages, Context context) {
this.listData = listData;
this.Images = listImages;
this.context = context;
}
最后,在您的onBindViewHolder中进行如下更改:
holder.imgView.setImageDrawable(ContextCompat.getDrawable(context, Images.get(position)));
就是这样。应该在recyclerview的每个项目中显示图像。尝试一下。