recyclerview No adapter attached; skipping layout

后端 未结 30 3167
走了就别回头了
走了就别回头了 2020-11-21 04:51

Just implemented RecyclerView in my code, replacing ListView.

Everything works fine. The data is displayed.

But error messages are

30条回答
  •  清酒与你
    2020-11-21 05:16

    I was getting the same two error messages until I fixed two things in my code:

    (1) By default, when you implement methods in the RecyclerView.Adapter it generates:

    @Override
    public int getItemCount() {
        return 0;
    }
    

    Make sure you update your code so it says:

    @Override
    public int getItemCount() {
        return artists.size();
    }
    

    Obviously if you have zero items in your items then you will get zero things displayed on the screen.

    (2) I was not doing this as shown in the top answer: CardView layout_width="match_parent" does not match parent RecyclerView width

    //correct
    LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_listitem, parent, false);
    
    //incorrect (what I had)
    LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_listitem,null);
    

    (3) EDIT: BONUS: Also make sure you set up your RecyclerView like this:

    
    

    NOT like this:

    
    

    I have seen some tutorials using the latter method. While it works I think it generates this error too.

提交回复
热议问题