将图像从drawable设置为recyclerview

时间:2018-02-14 06:05:23

标签: android android-recyclerview recycler-adapter

我想在底部导航片段中实现RecyclerView,其中包含带有TextView和ImageView的CardView。我在每张打开新活动的卡上都有一个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();
    }
 }

MainActivity

     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

1 个答案:

答案 0 :(得分:3)

您没有打电话给RecyclerViewAdapter构造函数:

通过将此行添加到您的片段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;