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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With