为了账号安全,请及时绑定邮箱和手机立即绑定

未连接适配器;跳过布局跳过 1 到 2 帧

未连接适配器;跳过布局跳过 1 到 2 帧

摇曳的蔷薇 2024-01-28 16:45:04
我尝试寻找答案,但没有任何效果,但我相信这段代码是有问题的,调试器说这是我的文件的链接:TodoListApp跳过 1 帧!应用程序可能在其主线程上做了太多工作// 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();        }    });
查看完整描述

1 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

在你的 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 的数据集:


public void updateData(ArrayList<MyDataType> newDataSet){

    myAdapterDataSet = newDataSet;


    // Let the Adapter know the data has changed and the view should be refreshed

    notifyDataSetChanged();

}


查看完整回答
反对 回复 2024-01-28
  • 1 回答
  • 0 关注
  • 30 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信