回收者视图未在活动中显示任何内容

时间:2019-05-06 08:11:19

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

我从firebase获得了书籍的详细信息,并将其添加到“ BookList”列表中。添加书籍后,我设置了回收站视图和适配器,但是当我运行该应用程序时,它什么也没显示。我不知道我的代码出了什么问题

任何帮助将不胜感激。

1)WishListActivity

public class WishListActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    FirebaseUser CurrentUser;
    String CurrentUserEmail;
    WishListAdapter wishListAdapter;

    List<BooksModel> BookList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wish_list);

        CurrentUser = FirebaseAuth.getInstance().getCurrentUser();
        CurrentUserEmail = CurrentUser.getEmail();
        recyclerView = findViewById(R.id.recyclerView_WishList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        BookList = new ArrayList<>();

        DatabaseReference WishListRef = 
        FirebaseDatabase.getInstance().getReference("wishlist");
        WishListRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    for(DataSnapshot 
        WishListSnapshot:dataSnapshot.getChildren()){


                        WishListModel Model = 
        WishListSnapshot.getValue(WishListModel.class);

                        if(Model.getEmail().equals(CurrentUserEmail)){

                            DatabaseReference BookReference = 





 FirebaseDatabase.getInstance().getReference("Books").child(Model.getKey());
                            BookReference.addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                    BooksModel Book = dataSnapshot.getValue(BooksModel.class);
                                    BookList.add(Book);
                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError databaseError) {

                                }
                            });

                        }
                        else{
                            Toast.makeText(WishListActivity.this, "Email Not Matched! ", Toast.LENGTH_SHORT).show();
                        }

                    }
                    wishListAdapter = new WishListAdapter(WishListActivity.this,BookList);
                    recyclerView.setAdapter(wishListAdapter);

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

WishListAdapter

public class WishListAdapter extends RecyclerView.Adapter<WishListAdapter.WishListViewHolder>{
    private Context context;
    private List<BooksModel> mDatalist;

    public WishListAdapter(Context context, List<BooksModel> mDatalist) {
        this.context = context;
        this.mDatalist = mDatalist;
    }

    @NonNull
    @Override
    public WishListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.wish_list,viewGroup
        ,false);
        WishListViewHolder wishListViewHolder = new WishListViewHolder(rootView);
        return wishListViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull WishListViewHolder wishListViewHolder, int i) {

        final BooksModel Book = mDatalist.get(i);

        wishListViewHolder.BookName.setText(Book.getName());
        wishListViewHolder.BookPrice.setText(Book.getPrice());
        wishListViewHolder.AuthorName.setText(Book.getAuthor());
        wishListViewHolder.RemoveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Remove Button Clicked! ", Toast.LENGTH_SHORT).show();
            }

        });

    }

    @Override
    public int getItemCount() {
        return mDatalist.size();
    }

    public class WishListViewHolder extends  RecyclerView.ViewHolder{

        TextView BookName,BookPrice,AuthorName;
        Button RemoveButton;



        public WishListViewHolder(@NonNull View itemView) {
            super(itemView);

            BookName = itemView.findViewById(R.id.book_price_wish_list);
            BookPrice = itemView.findViewById(R.id.book_price_wish_list);
            RemoveButton = itemView.findViewById(R.id.remove_book_wish_list);

        }
    }

}

愿望清单活动代码

4 个答案:

答案 0 :(得分:2)

如果不设置适配器大小,则必须在适配器类中设置适配器大小,然后如何在适配器类中显示项目,只需像这样在适配器类中更改getItemCount即可:-

@Override
public int getItemCount() {
    return 0;
}

并在此处设置列表大小,如下所示:-

@Override
public int getItemCount() {
    return mDatalist.size();
}

答案 1 :(得分:1)

您必须在适配器类中设置适配器数组列表的大小。在您的适配器类中更改此代码

@Override
public int getItemCount() {
    return 0;
}

在下面更新您的捐赠。

@Override
public int getItemCount() {
     return mDatalist.size();
}

答案 2 :(得分:0)

在WishListAdapter中是错误的,

@Override
public int getItemCount() {
    return mDatalist.size();
}

这将使recyclerView和适配器不呈现任何项目。

应该是

.table-responsive

答案 3 :(得分:0)

问题是

  @Override
    public int getItemCount() {
        return 0;
    }

像这样返回原始列表大小

@Override
        public int getItemCount() {
            return mDatalist.size();
        }