简体   繁体   English

我的 recyclerview 需要来自不同线程中的数据库访问的数据,但随后我得到“未连接适配器”

[英]I need data from a database access in a different thread for my recyclerview, but then I get “No adapter attached”

I want to populate my RecyclerView, which from what I've heard, needs to be done in the main thread (?).我想填充我的 RecyclerView,据我所知,这需要在主线程(?)中完成。 But I want to populate said RecyclerView with info from my Room, which said query cannot be done in the main thread.但我想用来自我房间的信息填充所说的 RecyclerView,其中所说的查询不能在主线程中完成。 How do I get around this?我该如何解决这个问题? I've already tried running all ui related actions in a runOnUiThread():我已经尝试在 runOnUiThread() 中运行所有与 ui 相关的操作:

public void updateTeams(){
        new Thread(new Runnable() {
            public void run() {
                AppDatabase db = AppDatabase.getInstance(getApplicationContext());
                final List<Team> allTeams = db.teamDao().getAll();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        RecyclerView recyclerView = findViewById(R.id.teamRecyclerView);
                        TeamRecyclerViewAdapter adapter = new TeamRecyclerViewAdapter(TeamsActivity.this, allTeams);
                        recyclerView.setAdapter(adapter);
                        recyclerView.setLayoutManager(new LinearLayoutManager(TeamsActivity.this));
                    }
                });
            }
        }).start();
    }

I get E/RecyclerView: No adapter attached; skipping layout我得到E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No adapter attached; skipping layout and an unpopulated recyclerview E/RecyclerView: No adapter attached; skipping layout和未填充的回收视图

updateTeams() is called in the onCreate() of the activity. updateTeams() 在活动的 onCreate() 中被调用。 It is also called in onActivityResult() in the same activity.它也在同一活动中的 onActivityResult() 中调用。 Does that make a difference?这有什么区别吗?

Try to initialize your RecyclerView and Adapter inside onCreate like below:尝试在onCreate中初始化 RecyclerView 和 Adapter,如下所示:

private RecyclerView recyclerView;
private TeamRecyclerViewAdapter adapter;
private List<Team> allTeams;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    allTeams = new ArrayList<>();
    RecyclerView recyclerView = findViewById(R.id.teamRecyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    TeamRecyclerViewAdapter adapter = new TeamRecyclerViewAdapter(this, allTeams);
    recyclerView.setAdapter(adapter);

    ...
}

And inside runnable just update the data list and notify the adapter like below:在 runnable 中,只需更新数据列表并通知适配器,如下所示:

public void run() {
    AppDatabase db = AppDatabase.getInstance(getApplicationContext());
    allTeams = db.teamDao().getAll();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            adapter.notifyDataSetChanged();
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在回收站视图中显示数据时出错:RecyclerView:未连接适配器; 跳过布局 - error when i display my data in recycler view :RecyclerView: No adapter attached; skipping layout 我需要从后台线程访问列表视图的适配器 - I need to access adapter of listview from a background thread 如何使用 AsyncTask、RecyclerView 和 RecyclerView.Adapter 将手机中的图像获取到 RecyclerView? - How can I use AsyncTask, RecyclerView, and a RecyclerView.Adapter to get images from my phone into my RecyclerView? Android 工作室从 Firebase 检索数据并得到错误 E/RecyclerView:没有连接适配器; 跳过布局 - Android studio retrieve data from Firebase and get error E/RecyclerView: No adapter attached; skipping layout 我正在获取E / RecyclerView:未连接适配器; 跳过布局而不返回数据,我在做什么错? - Im getting E/RecyclerView: No adapter attached; skipping layout and not returning the data, what am i doing wrong? 我想从Firebase数据库到Firebase Recyclerview适配器获取订单详细信息 - I want to get order details from firebase database to firebase recyclerview adapter 如何从 RecyclerView Adapter 持有人访问数据库? - How to access database from RecyclerView Adapter holder? 如何从sqlite数据库附加RecyclerView适配器 - How do I attach RecyclerView adapter from sqlite database 我应该如何处理错误的RecyclerView:未连接适配器; 跳过布局 - What Should I Do with the error RecyclerView: No adapter attached; skipping layout 如何从适配器外部获取我的回收站视图中显示的项目的位置? - How do I get the position of the item shown in my recyclerview from outside of the adapter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM