27

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 recyclerView = (RecyclerView)findViewById(R.id.rec);
List<MenuData> list = new ArrayList<>();
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        MenuRecAdapter menuRecAdapter = new MenuRecAdapter(list);
        recyclerView.setAdapter(menuRecAdapter);

RecyclerView adapter:

    public class MenuRecAdapter extends RecyclerView.Adapter<RecViewHolder>{

    private List<MenuData> mList;
    Activity context;

    public MenuRecAdapter(List<MenuData> mList){
        this.mList = mList;
    }

    public int getItemCount(){
        return mList.size();
    }

    public RecViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.menuitem, viewGroup, false);
        RecViewHolder pvh = new RecViewHolder(v);
        return pvh;
    }

    public void onBindViewHolder(RecViewHolder holder, int i){
        holder.menuTeXT.setText(mList.get(i).text);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

}

ViewHolder of the items:

  public class RecViewHolder extends RecyclerView.ViewHolder {

    public TextView menuTeXT;

    public RecViewHolder(View itemView){
        super(itemView);

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

and the data I want to put into my recyclerview (what doesn't show):

    class MenuData {

    String text;

    MenuData(String text){
        this.text = text;
    }

    private List<MenuData> list;

    private void initializeData(){
        list = new ArrayList<>();
        list.add(new MenuData("Featured"));
        list.add(new MenuData("Categories"));
        list.add(new MenuData("Sell"));
        list.add(new MenuData("Settings"));
        list.add(new MenuData("Logout"));
    }

}

Thanks in advance

svenvdz
  • 623
  • 2
  • 8
  • 16

3 Answers3

51

In MainActivity

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

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

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

MenuRecAdapter menuRecAdapter = new MenuRecAdapter(list);
recyclerView.setAdapter(menuRecAdapter);

RecyclerView Adapter

public class MenuRecAdapter extends RecyclerView.Adapter<RecViewHolder>{

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

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

public int getItemCount(){
    return mList.size();
}

public RecViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){

    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.menuitem, viewGroup, false);
    RecViewHolder pvh = new RecViewHolder(v);
    return pvh;
}

public void onBindViewHolder(RecViewHolder holder, int i){
    holder.menuTeXT.setText(mList.get(i));
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}

and ViewHolder remains same...

public class RecViewHolder extends RecyclerView.ViewHolder {

public TextView menuTeXT;

public RecViewHolder(View itemView){
    super(itemView);

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

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

Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
  • 5
    Yeah, LinearLayoutManager is important here. – Artem M Jul 06 '18 at 17:35
  • 8
    This is a poorly written answer that doesn't explain at all what the fix is, just a bunch of code. How did this answer get 53 upvotes? The problem was simply that the list was empty? – satur9nine Oct 31 '19 at 21:10
4

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:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(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.

ivan-abc
  • 41
  • 2
  • Thank you, dude! Literally, you've made my day. Spent about 8 hours figuring out what's wrong with my custom RecyclerView adapter. The issue was that I haven't specified layout manager to recycler view itself. Official docs mentions about layout manager, but never shows how to set it up. – Geradlus_RU Jul 30 '21 at 17:36
0

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

yongsunCN
  • 736
  • 7
  • 18