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

坚强是说给别人听的谎言 提交于 2019-12-11 19:37:26

问题


public class ToDoAdapter extends RecyclerView.Adapter<ToDoAdapter.MyViewHolder> {

    Context context;
    ArrayList<ToDoItems> toDoItems;


    public ToDoAdapter(Context c, ArrayList<ToDoItems> p) {
        context = c;
        toDoItems = p;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.front_page_layout, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.itemTitle.setText(toDoItems.get(i).getItemTitle());
        myViewHolder.itemSubject.setText(toDoItems.get(i).getItemSubject());
        myViewHolder.itemDueDate.setText(toDoItems.get(i).getItemDueDate());

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView itemTitle;
        TextView itemSubject;
        TextView itemDueDate;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            itemTitle = itemView.findViewById(R.id.itemTitle);
            itemSubject = itemView.findViewById(R.id.itemSubject);
            itemDueDate = itemView.findViewById(R.id.itemDueDate);

        }
    }

}

My Main.class file

       //Working with data
            mainPageRecyclerView = findViewById(R.id.mainPageRecyclerView);
            mainPageRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            list = new ArrayList<ToDoItems>();


            addNewTask.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent a = new Intent(MainActivity.this,NewTaskAct.class);
                    startActivity(a);
                }
            });

            //get data from firebase
            dbReference = FirebaseDatabase.getInstance().getReference().child("to-do-list");
            dbReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    //set code to retrieve data and replace layout
                    for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                    {
                        ToDoItems p = dataSnapshot1.getValue(ToDoItems.class);
                        list.add(p);
                    }
                    toDoAdapter = new ToDoAdapter(MainActivity.this, list);
                    mainPageRecyclerView.setAdapter(toDoAdapter);
                    toDoAdapter.notifyDataSetChanged();
                }

I can see the data in the firebase database but it will not pull the information back from the database and i just get the following error in android studio RUN --> E/RecyclerView: No adapter attached; skipping layout I am writing a simple to do app as part of my learning and i can write information to firebase but i keep getting the above error when my app changes views and needs to call data from the database

I have youtube'd and looked through other stack overflow questions but I can't understand a solution that will work with my project


回答1:


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();


来源:https://stackoverflow.com/questions/58034951/e-recyclerview-no-adapter-attached-skipping-layout-when-making-a-todo-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!