未连接适配器;跳过布局跳过1至2帧 [英] No adapter attached; skipping layout skipped 1 till 2 frames

查看:65
本文介绍了未连接适配器;跳过布局跳过1至2帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试寻找答案,但是没有任何效果,但是我相信这段代码是有问题的,调试器说

I have tried looking for the answer but none works but i believe that this code is a problem,debugger says that

这是我文件的链接: TodoListApp

跳过1帧!该应用程序可能在其主线程上做过多的工作

Skipped 1 frames! The application may be doing too much work on its main thread

// working with data
    ourdoes = findViewById(R.id.ourdoes);
    ourdoes.setLayoutManager(new LinearLayoutManager(this));
    list = new ArrayList<MyDoes>();

    // get data from firebase
    reference = FirebaseDatabase.getInstance().getReference().child("SeaLab13");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // set code to retrive data and replace layout
            for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
            {
                MyDoes p = dataSnapshot1.getValue(MyDoes.class);
                list.add(p);
            }
            doesAdapter = new DoesAdapter(MainActivity.this, list);
            ourdoes.setAdapter(doesAdapter);
            doesAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // set code to show an error
            Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_SHORT).show();
        }
    });

推荐答案

在onCreate内部:

Inside your onCreate:

// Set up your RecyclerView with the appropriate Layout Manager
RecyclerView myRecycler = findViewById(R.id.my_recycler_id);
myRecycler.setLayoutManager(new LinearLayoutManager(this));

// Create your data set
myData = new ArrayList<MyDataType>();

// Create an instance of your adapter passing the data set into the constructor
myAdapter = new MyAdapter(this, myData);

// Set the Adapter on the RecyclerView directly within onCreate
// so that it doesn't get skipped
myRecycler.setAdapter(myAdapter);

在事件监听器回调中:

@Override
public void onDataChange(DataSnapshot snapshot){
    // Add the new data to your data set ex. myData.add(newData)
    // ...

    // After adding to the data set,
    // update the data using a custom function you define in your Adapter's class
    myAdapter.updateData(myData);
}

在Adapter类中,创建一个函数来更新Adapter的数据集:

Inside your Adapter class, create a function to update your Adapter's data set:

public void updateData(ArrayList<MyDataType> newDataSet){
    myAdapterDataSet = newDataSet;

    // Let the Adapter know the data has changed and the view should be refreshed
    notifyDataSetChanged();
}

这篇关于未连接适配器;跳过布局跳过1至2帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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