0
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

Victor Kim
  • 1,647
  • 2
  • 16
  • 33
Mr J
  • 3
  • 3
  • Put the debugger inside `onDataChange` and see if your execution ever reaches there at all. – kushpf Sep 20 '19 at 21:49
  • Possible duplicate of [recyclerview No adapter attached; skipping layout](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – Sammy T Sep 21 '19 at 00:47
  • Ok So I did the log D and it seemed that it is skipping the For loop in my on data change section. see log 2019-09-21 19:56:14.824 32067-32067/com.example.todolist D/MainActivity Debugging: onDataChange: Start 2019-09-21 19:56:14.824 32067-32067/com.example.todolist D/MainActivity Debugging: onDataChange: after for loop 2019-09-21 19:56:15.674 32067-32067/com.example.todolist D/MainActivity Debugging: onDataChange: Start 2019-09-21 19:56:15.674 32067-32067/com.example.todolist D/MainActivity Debugging: onDataChange: after for loop – Mr J Sep 21 '19 at 18:58
  • Also not the same as Possible duplicate of recyclerview No adapter attached; skipping layout because mine isnt displaying the information at all, just giving the error. – Mr J Sep 21 '19 at 19:27
  • @MrJ Even if your For loop executes, the Recyclerview still won't show anything because there's no adapter at the time the layout is being drawn. See my answer to fix the Recyclerview being skipped. – Sammy T Sep 22 '19 at 05:17

1 Answers1

0

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();
Sammy T
  • 1,924
  • 1
  • 13
  • 20
  • I tried and somehow broke the button that should bring me back to the main screen, it's skipping code. I have done an invalidate and restart but i'm having no joy. Heres me reaching into my pocket and going to buy a Udemy course. O_o.. Edit.... I just got my button working again. let me try your solution again... – Mr J Sep 23 '19 at 14:15