E/RecyclerView:没有连接适配器;跳过布局 [英] E/RecyclerView﹕ No adapter attached; skipping layout

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

问题描述

通过这个问题的标题,很容易理解,recyclerview 的适配器没有设置在 UI 线程中.但在我的情况下,为了避免我什至在 UI 线程上尝试这样做,但仍然没有运气.

By the title of this question, its easily understandable that, the adapter of recyclerview is not set inside a UI thread. But in my case to avoid that I have tried doing it even on UI thread but still no luck.

我正在为我的应用使用 FirebaseUI.下面是代码片段:

I am using FirebaseUI for my app. Below is the code snippet:

public static void getUserFromUserId(@NonNull DatabaseReference dbRef, @NonNull String userId) {
    dbRef.child(USERS).child(userId)
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);
                    FriendsActivity.this.runOnUiThread(new Handler() {
                        @Override
                        public void run() {
                            loadFriends(user);
                        }
                    });

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    FirebaseCrash.report(databaseError.toException());
                }
            });
}

private void loadFriends(User user) {
    Query friendsRef = ;  // your firebase DatabseReference path
    FirebaseRecyclerAdapter<Friend, FriendViewHolder> adapter =
            new FirebaseRecyclerAdapter<Friend, FriendViewHolder>(Friend.class,
                    R.layout.item_challenge, FriendViewHolder.class, friendsRef) {
                @Override
                protected void populateViewHolder(FriendViewHolder viewHolder, Friend model, int position) {
                    viewHolder.setFriendName(model.getName());
                    viewHolder.setFriendPic(FriendActivity.this, model.getProfilePic());
                }
            };
    mRecyclerView.setAdapter(adapter);
}

我的 Activity 的 onCreate() 方法有以下与 RecyclerView 相关的代码:

My Activity's onCreate() method has below code related to RecyclerView:

mRecyclerView = (RecyclerView) findViewById(R.id.challenge_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(layoutManager);

我不明白为什么即使在 runOnUiThread 中调用了 loadFriends 之后,为什么错误仍然存​​在.

I dont understand why even after calling loadFriends inside runOnUiThread, why the error still persists.

感谢任何帮助.

推荐答案

初始化RecyclerView时需要附加adapter,附加LayoutManager等

You need to attach your adapter when you initialize your RecyclerView and attach LayoutManager, etc.

loadFriends 方法应该获取数据并将数据添加到适配器,然后您应该调用 notifyDataSetChanged 或等效方法.

loadFriends method should fetch data and add data to the adapter and then you should call notifyDataSetChanged or equivalent.

你在这里做的是不正确的.recyclerview 应该始终附加一个适配器.您只需要更新适配器中的数据.

What you're doing here is incorrect. A recyclerview should always have an adapter attached. You just need to update the data in the adapter.

这就是错误所说的 E/RecyclerView:没有连接适配器;跳过布局.因为你在附加LayoutManager之后还没有附加适配器,而你是在后期附加适配器.

And that's what the error says E/RecyclerView﹕ No adapter attached; skipping layout. Because you have not attached adapter after attaching LayoutManager and you're attaching adapter at a later stage.

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

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