Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set image from drawable to recyclerview

I want to implement RecyclerView in bottom navigation fragment that contains CardViews with a TextView and an ImageView. I also have an onClickListener on every card that open new activity. but my recyclerview don't show anything and gives null point exception Logcat.I don't know what I am missing here.

Model Class

   class Data {

public int imageId;
public String txt;

Data( int imageId, String text) {

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

RecyclerView Adapter

    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

like image 312
Mrunal Avatar asked Feb 14 '18 06:02

Mrunal


1 Answers

You are not calling RecyclerViewAdapter Constructor :

call by add this line to your 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;
like image 88
Abhishek kumar Avatar answered Oct 22 '22 13:10

Abhishek kumar