I have a CardView with one ImageView and three TextView , however they do not appear when the application starts. How to add the code correctly to make it work? Here is the fragment code where CardView should be

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { RecyclerView recyclerView = (RecyclerView) inflater.inflate( R.layout.recycler_view, container, false); ContentAdapter adapter = new ContentAdapter(recyclerView.getContext()); recyclerView.setAdapter(adapter); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); return recyclerView; } // toolbar @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); // This Fragment has a menu } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){ inflater.inflate(R.menu.menu_news, menu); // Inflate the Fragment's menu super.onCreateOptionsMenu(menu, inflater); } // recycler public static class ViewHolder extends RecyclerView.ViewHolder { CardView cv; TextView cvUser; TextView cvLocation; ImageView cvPhoto; TextView cvContent; public ViewHolder(LayoutInflater inflater, ViewGroup parent) { super(inflater.inflate(R.layout.card_feed, parent, false)); cv = (CardView) itemView.findViewById(R.id.card_view); cvUser = (TextView) itemView.findViewById(R.id.card_user); cvLocation = (TextView) itemView.findViewById(R.id.card_location); cvPhoto = (ImageView) itemView.findViewById(R.id.card_image); cvContent = (TextView) itemView.findViewById(R.id.card_text); } } public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> { // Установим количество элементов списка в RecyclerView. private static final int LENGTH = 18; public ContentAdapter(Context context) { } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(LayoutInflater.from(parent.getContext()), parent); } @Override public void onBindViewHolder(ViewHolder holder, int position) { // no-op } @Override public int getItemCount() { return LENGTH; } } 

    1 answer 1

    In order for this implementation of RecyclerView work, you need to associate the ViewHolder in the onBindViewHolder method with the received data. And then, no data for the bundle is transferred to the ContentAdapter class, there is nothing to display on the screen.

    • It is assumed that you should have a class-model (Class Model) in your project where the image and text data are stored. This data in the form of a list should be passed to the constructor of your adapter, and then further implement the data connection in onBindViewHolder: Model model = listData.get (position); holder.bindData (model); and further, already in your ViewHolder class: paint the method void bindData (Model model) {cv.setText (model.getText ()); // and so on} - cosmic_M
    • Is there an example to make it easier? - 3Jlou 4uTep
    • Download the book Hardy B., Phillips B., Stuart K., Marsicano K. - Android. Programming for professionals, 2016. There is a chapter detailing the implementation of RecyclerView - cosmic_M