Android RecycleView不会在列表中显示数据

时间:2016-12-01 10:53:25

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

 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);

1 个答案:

答案 0 :(得分:2)

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

salesBookAdapter.notifyDataSetChanged()

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