【问题标题】:Android RecycleView does not show data in listAndroid RecyclerView 不显示列表中的数据
【发布时间】:2017-04-15 23:29:01
【问题描述】:
 this.books = new ArrayList<>();
            mFirebaseDatabaseReference.child(SellBooksPost).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange( DataSnapshot AdataSnapshot) {

                    for (final DataSnapshot AmessageSnapshot : AdataSnapshot.getChildren()) {

                        bFirebaseDatabaseReference.child(mUserId).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot BdataSnapshot) {

                            for (DataSnapshot BmessageSnapshot : BdataSnapshot.getChildren()) {
                                if (BmessageSnapshot.child("book").getValue().toString().equals(AmessageSnapshot.child("SellBooksPostId").getValue().toString())){
                                    Log.d("INFO", "SELL BOOK book " + BmessageSnapshot.child("book").getValue());
                                    Log.d("INFO", "SELL BOOK DETAIL " + AmessageSnapshot.child("SellBooksPostId").getValue());

                                    books.add(new Book(

                                            AmessageSnapshot.child("SellBooksPostId").getValue().toString(),
                                            AmessageSnapshot.child("bookTitle").getValue().toString()
                                    ));

                                }
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });

                }

                saleBookPostAdapter = new SaleBookPostAdapter(getApplicationContext(), books);
                mRoomRecyclerView.setHasFixedSize(true);
                mRoomRecyclerView.setAdapter(saleBookPostAdapter);

【问题讨论】:

  • 在提供代码之前,请简要描述问题

标签: android firebase android-recyclerview firebase-realtime-database


【解决方案1】:

很难说这是否是个问题,但更新数据后我没有看到对RecyclerView.Adapter#notifyDataSetChanged() 方法的调用。所以尝试调用

salesBookAdapter.notifyDataSetChanged()

之后

books.add(new Book(
     AmessageSnapshot.child("SellBooksPostId").getValue().toString(),
     AmessageSnapshot.child("bookTitle").getValue().toString()
));

【讨论】:

  • 谢谢 andrei_zaitcev。我只是解决这个问题。我在函数外的 setadapter 上犯了愚蠢的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 2021-06-02
  • 1970-01-01
相关资源
最近更新 更多