E/RecyclerView: No adapter attached; skipping layout — when making a todo list app

后端 未结 1 1584
走了就别回头了
走了就别回头了 2021-01-26 15:17
public class ToDoAdapter extends RecyclerView.Adapter {

    Context context;
    ArrayList

        
相关标签:
1条回答
  • 2021-01-26 15:52

    Your Recyclerview is being skipped because there is no adapter attached to it when the layout is drawn. You're currently setting the adapter from an event callback which will still return after the layout is already drawn and the Recyclerview has been skipped. You need to call setAdapter() directly in your Activity's onCreate then update the adapter's data making sure to call notifydatasetchanged in your event callback.

    Inside your activity's onCreate:

    // Create an empty adapter since you don't have initial data
    // (You may need to alter the constructor of your Adapter class 
    //  to keep it from trying to process empty/null data so it doesn't break)
    MyAdapter myAdapter = new MyAdapter(null);
    
    // Set the Recyclerview's Adapter so it isn't skipped on the layout pass
    myRecyclerView.setAdapter(myAdapter);
    

    Then, inside your event callback:

    // Update your Adapter with the new data using an update function you define in your Adapter
    myAdapter.updateData(myNewData);
    
    // Notify the Adapter that the data has changed
    myAdapter.notifyDataSetChanged();
    
    0 讨论(0)
提交回复
热议问题