Skip to content Skip to sidebar Skip to footer

Android Recyclerview Doesn't Display Items

I want to show these items inside my recyclerview but it doesn't show at all and I can't see the error. Maybe you guys can help me out. MainActivity.java RecyclerView recyclerV

Solution 1:

In MainActivity

ArrayList<String> list = newArrayList<>();
list.add("something1");
list.add("something2");

RecyclerViewrecyclerView= (RecyclerView)findViewById(R.id.rec);
recyclerView.setHasFixedSize(true);

LinearLayoutManagerlinearLayoutManager=newLinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);

MenuRecAdaptermenuRecAdapter=newMenuRecAdapter(list);
recyclerView.setAdapter(menuRecAdapter);

RecyclerView Adapter

publicclassMenuRecAdapterextendsRecyclerView.Adapter<RecViewHolder>{

private ArrayList<String> mList = newArrayList<>();
Activity context;

publicMenuRecAdapter(ArrayList<String> mList){
    this.mList = mList;
}

publicintgetItemCount(){
    return mList.size();
}

public RecViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){

    Viewv= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.menuitem, viewGroup, false);
    RecViewHolderpvh=newRecViewHolder(v);
    return pvh;
}

publicvoidonBindViewHolder(RecViewHolder holder, int i){
    holder.menuTeXT.setText(mList.get(i));
}

@OverridepublicvoidonAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}

and ViewHolder remains same...

publicclassRecViewHolderextendsRecyclerView.ViewHolder {

public TextView menuTeXT;

publicRecViewHolder(View itemView){
    super(itemView);

    menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT);
}
}

also get rid of MenuData class. The above code should work fine.

Solution 2:

As it was mentioned above in comments, the problem might be in non-specifying the layoutManager attribute of the RecyclerView.

The layoutManager can be specified either in XML-file or dynamically in Java code.

Example of Java code from the answer above:

LinearLayoutManagerlinearLayoutManager=newLinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);

To add layoutManager via XML use the appropriate attribute:

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
         
   app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

This fixed problem for me.

Solution 3:

Since you are not getting any data, I guess the problem happens at the time you pass the list into your adapter. I see you didn't make copy of your list, so you are passing the reference of the list directly into the adapter. I suggest you to try MenuRecAdapter menuRecAdapter = new MenuRecAdapter(new Arraylist(list));

Post a Comment for "Android Recyclerview Doesn't Display Items"