gpt4 book ai didi

java - 预期在 onBindViewHolder 上找到数组类型?

转载 作者:行者123 更新时间:2023-11-29 20:53:33 27 4
gpt4 key购买 nike

我正在尝试使用 RecyclerView 显示一个左侧有图像、右侧有标题的列表。

通过 for 循环,我在 ArrayList 中添加了标题和图像

String[] mtitles = {"Title","Title","Title","Title","Title","Title","Title","Title","Title","Title"};
int[] mimages = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};

List<Feed> items = new ArrayList<Feed>();
Feed feed = new Feed();
for (int i=0;i<mtitles.length;i++)

{
feed.setImage(mimages[i]);
feed.setTitle(mtitles[i]);
items.add(feed);
}

adapter = new MyAdapter(items);

这是我的 Feed.class

public class Feed {

String title;
int image;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getImage() {
return image;
}

public void setImage(int image) {
this.image = image;
}
}

MyAdapter.class

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

public List<Feed> items;

public static class ViewHolder extends RecyclerView.ViewHolder
{
public ImageView icon;
public TextView title;

public ViewHolder(View v)
{
super(v);
icon = (ImageView)v.findViewById(R.id.imageView);
title = (TextView)v.findViewById(R.id.textView);


}

}


public MyAdapter(List<Feed> items)
{
this.items=items;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
ViewHolder vh = new ViewHolder(v);
return vh;

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

holder.icon.setImageResource(items[position]); // Problem here

}



@Override
public int getItemCount() {
return items.size();
}
}

在这条线上,

 holder.icon.setImageResource(items[position]);

我遇到了错误。

 Array type expected;found.

截图:

enter image description here

最佳答案

使用

holder.icon.setImageResource(items.get(position).getImage());

因为 itemsFeed 类对象的 List 而不是 Array

关于java - 预期在 onBindViewHolder 上找到数组类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28425414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com