0

Этот код является из slidenerd учебник для recyclerview им пытаются привязать данные к recyclerview с использованием getdata функцииНет данных внутри пользовательского адаптера

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; 
} 

последней итерации в цикл показывает 4 элементов в menuData списке но iAdapter показывает размер данных = 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); 
     } 
    } 
} 
+0

разместим ваш адаптер –

+0

Добавлен @Rod_Algonquin. – saurabh64

ответ

2

Проблема заключается в том, что, которого не ссылаться на список данных, которые вы только что прошли в конструкторе поэтому список данных пуст:

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

Это должно быть

public InformationAdapter(Context context, List<Information> data) { 
    inflator= LayoutInflater.from(context); 
    this.data = data; 
} 
Смежные вопросы