0

I'm new to android studio . I'm trying to display items in RecycleView but it's doesn't show anything.

This is what android studio says:


E/RecyclerView: No layout manager attached; skipping layout

My Fragment :

public class Articles extends Fragment {
    View root;
    RecyclerView recyclerView;
    NewsList[] myListData = new NewsList[] {
            new NewsList("Article 1", "Some Author"),
            new NewsList("Article2", "Some Author"),
            new NewsList("Article3", "Some Author"),

    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);






    }
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_articles, container, false);
        recyclerView = root.findViewById(R.id.recyclerView);
        NewsAdapter adapter = new NewsAdapter(myListData);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);


        return root;
    }

Here is the code of my Adapter class :

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
    private NewsList[] listdata;

    // RecyclerView recyclerView;
    public NewsAdapter(NewsList[] listdata) {

        this.listdata = listdata;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(listItem);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final NewsList myListData = listdata[position];
        holder.textView1.setText(listdata[position].getTitle());
        holder.textView2.setText(listdata[position].getAuthor());
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(),"click on item: "+myListData.getTitle(),Toast.LENGTH_LONG).show();
            }
        });
    }


    @Override
    public int getItemCount() {
        return listdata.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView1;
        public TextView textView2;
        public RelativeLayout relativeLayout;
        public ViewHolder(View itemView) {
            super(itemView);
            this.textView1 = (TextView) itemView.findViewById(R.id.textView1);
            this.textView2 = (TextView) itemView.findViewById(R.id.textView2);
            relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relativeLayout);
        }
    }

Previously I had an error where the app crashed when opened the fragment but now it's skips the RecyclerView. I will be gratefull if you help me to resolve this issue.

MayWheather
  • 476
  • 5
  • 18
  • Does this answer your question? [recyclerview No adapter attached; skipping layout](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – Biscuit Dec 18 '20 at 17:27

2 Answers2

1

You need to add a layoutManager to your recycler view:

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_articles, container, false);
        recyclerView = root.findViewById(R.id.recyclerView);
        NewsAdapter adapter = new NewsAdapter(myListData);
        recyclerView.setHasFixedSize(true);
        // Add this line
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()))
        recyclerView.setAdapter(adapter);


        return root;
    }
lpizzinidev
  • 12,741
  • 2
  • 10
  • 29
  • 1
    Thank you for answering! It's worked. I knew that I needed LinearLayoutManager but I didn't know how to use it in my case since I'm new to android studio. Thank you for your help. :) – MayWheather Dec 18 '20 at 17:44
  • You'll always need to set a layout manager for a recycler view when you initialize it. There are different types of layout managers based on the type of layout you need to set for your list. LinearLayoutManager is the simplest of them. For more information see https://developer.android.com/guide/topics/ui/layout/recyclerview – lpizzinidev Dec 19 '20 at 09:34
1

It's mandatory to have a Layout manager for your Recyclerview. For example, you can use a linear one by adding this after inflating your view :

recyclerView.setLayoutManager(new LinearLayoutManager( context ));

where context is whatever your context is

Pylvain
  • 110
  • 1
  • 7
  • I tried the method and it said that it's wasn't the right context so I've tried Luca Pizzini method. Still thank you for your help :)! – MayWheather Dec 18 '20 at 17:46
  • My bad, the context here must be getActivity() and not "this". I'm programming in Kotlin, so it's not quite the same, but I ran in this exact problem yesterday – Pylvain Dec 18 '20 at 17:52