0

This code is from slidenerd tutorial for recyclerview im trying to bind the data to the recyclerview using getdata function

public static List<Information> getData()
{
    List<Information> menuData=new ArrayList<>();
    int[] icons={R.drawable.ic_bluetooth,R.drawable.ic_crosshairs_gps,R.drawable.ic_laptop,R.drawable.ic_remote};
    String[] titles={"Bluetooth","GPS","Laptop","Remote"};

    for(int i=0;i<titles.length&&i<icons.length;i++)
    {
        Information current =new Information();
        current.iconID=icons[i];
        current.title=titles[i];
        menuData.add(current);
    }

    return menuData;
}

last iteration in the for loop shows 4 items in the menuData list but the iAdapter shows data size=0

enter image description here

InformationAdapter

public class InformationAdapter extends RecyclerView.Adapter<InformationAdapter.infoViewholder> {

    private LayoutInflater inflator;
    List<Information> data=Collections.emptyList();

    public InformationAdapter(Context context, List<Information> data) {
       inflator= LayoutInflater.from(context);
    }

    @Override
    public infoViewholder onCreateViewHolder(ViewGroup parent, int i) {
       View view= inflator.inflate(R.layout.custom_row,parent,false);

        infoViewholder holder=new infoViewholder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(infoViewholder holder, int position) {
        Information current=data.get(position);
        holder.title.setText(current.title);
        holder.icon.setImageResource(current.iconID);

    }

    @Override
    public int getItemCount()
    {

        return data.size();
    }
    class infoViewholder extends RecyclerView.ViewHolder
    {
      TextView title;
        ImageView icon;

        public infoViewholder(View itemView) {

            super(itemView);
            title= (TextView) itemView.findViewById(R.id.list_text);
           icon= (ImageView) itemView.findViewById(R.id.text_icon);
        }
    }
}
saurabh64
  • 363
  • 1
  • 4
  • 24

1 Answers1

2

The problem is that you didnt reference the List of data you just passed in your constructor therefore the data list is empty:

 public InformationAdapter(Context context, List<Information> data) {
   inflator= LayoutInflater.from(context);
}

It should be

 public InformationAdapter(Context context, List<Information> data) {
   inflator= LayoutInflater.from(context);
   this.data = data;
}
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63