recyclerview No adapter attached; skipping layout

后端 未结 30 3062
走了就别回头了
走了就别回头了 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:29

    I had the same error I fixed it doing this if you are waiting for data like me using retrofit or something like that

    Put before Oncreate

    private ArtistArrayAdapter adapter;
    private RecyclerView recyclerView;
    

    Put them in your Oncreate

     recyclerView = (RecyclerView) findViewById(R.id.cardList);
     recyclerView.setHasFixedSize(true);
     recyclerView.setLayoutManager(new LinearLayoutManager(this));
     adapter = new ArtistArrayAdapter( artists , R.layout.list_item ,getApplicationContext());
     recyclerView.setAdapter(adapter);
    

    When you receive data put

    adapter = new ArtistArrayAdapter( artists , R.layout.list_item ,getApplicationContext());
    recyclerView.setAdapter(adapter);
    

    Now go in your ArtistArrayAdapter class and do this what it will do is if your array is empty or is null it will make GetItemCount return 0 if not it will make it the size of artists array

    @Override
    public int getItemCount() {
    
        int a ;
    
        if(artists != null && !artists.isEmpty()) {
    
            a = artists.size();
        }
        else {
    
            a = 0;
    
         }
    
       return a;
    }
    

提交回复
热议问题