E/RecyclerView:未连接适配器;跳过布局-制作待办事项列表应用程序时 [英] E/RecyclerView: No adapter attached; skipping layout -- when making a todo list app

查看:78
本文介绍了E/RecyclerView:未连接适配器;跳过布局-制作待办事项列表应用程序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

        }
    }

}

我的Main.class文件

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

我可以在firebase数据库中看到数据,但是它不会从数据库中拉回信息,而我在android studio RUN中仅收到以下错误-> E/RecyclerView:未连接适配器;跳过布局 作为学习的一部分,我正在编写一个简单的应用程序,可以将信息写入firebase,但是当我的应用程序更改视图并需要从数据库中调用数据时,我会不断遇到上述错误

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

我已经在YouTube上浏览并查看了其他堆栈溢出问题,但我无法理解适用于我的项目的解决方案

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

推荐答案

正在跳过您的Recyclerview,因为在绘制布局时没有附加适配器.当前,您正在通过事件回调设置适配器,该事件回调在绘制布局并跳过Recyclerview之后仍将返回.您需要直接在Activity的onCreate中调用setAdapter(),然后更新适配器的数据,以确保在事件回调中调用notifydatasetchanged.

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.

在活动的onCreate内:

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

然后,在事件回调中:

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

这篇关于E/RecyclerView:未连接适配器;跳过布局-制作待办事项列表应用程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆