gpt4 book ai didi

android - 将图像从 drawable 设置为 recyclerview

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:13 25 4
gpt4 key购买 nike

我想在包含带有 TextView 和 ImageView 的 CardView 的底部导航 fragment 中实现 RecyclerView。我在打开新 Activity 的每张卡片上也有一个 onClickListener。但我的 recyclerview 没有显示任何内容并给出空点异常 Logcat .我不知道我在这里错过了什么。

模型类

   class Data {

public int imageId;
public String txt;

Data( int imageId, String text) {

this.imageId = imageId;
this.txt = text;
}
}

RecyclerView 适配器

    public class RecyclerViewAdapter  extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
List<Data> data ;
Context context;

public RecyclerViewAdapter(Context context, ArrayList<Data>data) {
this.context = context;
this.data = data;
}

public static class MyViewHolder extends RecyclerView.ViewHolder{

public CardView mCardView;
public TextView mTextView;
public ImageView imageView;

public MyViewHolder(View v){
super(v);

mCardView = (CardView) v.findViewById(R.id.cardview1);
mTextView = (TextView) v.findViewById(R.id.tv_text);
imageView = (ImageView) v.findViewById(R.id.iv_image);

}
}

@Override
public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}

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

Data all = data.get(position);
holder.mTextView.setText(all.txt);
holder.imageView.setImageResource(all.imageId);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
final Intent intent;
if (position == 0){
intent = new Intent(context, ShowDetails.class);
} else if (position == 1){
intent = new Intent(context, AllDetails.class);
} else {
intent = new Intent(context, RecentDetails.class);
}
context.startActivity(intent);
}
});
}

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

主 Activity

     RecyclerView recyclerView;
RecyclerView.Adapter adapter;
private List<Data> data;

public StatusFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_status, container, false);

recyclerView = rootView.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
ArrayList<Data> data= new ArrayList<>();

data.add(new Data( R.drawable.bg, "Image 1"));
data.add(new Data( R.drawable.bg, "Image 2"));
data.add(new Data( R.drawable.bg, "Image 3"));
data.add(new Data( R.drawable.bg, "Image 1"));
data.add(new Data( R.drawable.bg, "Image 2"));
data.add(new Data( R.drawable.bg, "Image 3"));
data.add(new Data( R.drawable.bg, "Image 1"));
data.add(new Data( R.drawable.bg, "Image 2"));
data.add(new Data( R.drawable.bg, "Image 3"));

recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
return rootView;
}
}

ScreenShot

最佳答案

您没有调用 RecyclerViewAdapter 构造函数:

通过将此行添加到您的 Fragment adapter = new RecyclerViewAdapter (getActivity(), data);

RecyclerViewAdapter adapter;
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
adapter = new RecyclerViewAdapter (getActivity(), data);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootView;

关于android - 将图像从 drawable 设置为 recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48780647/

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