android recyclerview不显示项目

我想在回收站内显示这些项目,但是根本没有显示,我看不到错误。 也许你们可以帮我一把。

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适配器:

  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的项目:

  public class RecViewHolder extends RecyclerView.ViewHolder { public TextView menuTeXT; public RecViewHolder(View itemView){ super(itemView); menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT); } } 

和我想要放入我的recyclerview(什么不显示)的数据:

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

提前致谢

Solutions Collecting From Web of "android recyclerview不显示项目"

在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适配器

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

和ViewHolder保持不变…

 public class RecViewHolder extends RecyclerView.ViewHolder { public TextView menuTeXT; public RecViewHolder(View itemView){ super(itemView); menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT); } } 

也摆脱了MenuData类。 上面的代码应该可以正常工作。

由于您没有收到任何数据,我想这个问题发生在您将这个列表传递给您的适配器的时候。 我看到你没有复制你的列表,所以你直接将列表的引用传递给适配器。 我build议你尝试MenuRecAdapter menuRecAdapter = new MenuRecAdapter(new Arraylist(list));