Abdul Moiz
Abdul Moiz

Reputation: 49

Recycler View is not Showing anything on the activity

I got the details of the books from the firebase and added to the 'BookList' List. The books get added and I set the recycler view and adapter, but when I run the app it does not show anything. I don't know what's wrong with my code

Any help will be highly appreciated thanks.

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

        }
    }

}

code of the wishlist activity

Upvotes: 1

Views: 76

Answers (4)

Kaushal Panchal
Kaushal Panchal

Reputation: 1825

You have to set your adapter arraylist size in your adapter class. Change this code in your adapter class

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

Update your give below.

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

Upvotes: 1

Sandeep Malik
Sandeep Malik

Reputation: 1976

you have to set your adapter size in your adapter class if you will not set your adapter size then how can you show the items on your adapter just change your getItemCount in your adapter class like this :-

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

and set your list size here like this :-

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

Upvotes: 2

akshay_shahane
akshay_shahane

Reputation: 4633

problem is

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

return original list size like this

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

Upvotes: 0

Teffy
Teffy

Reputation: 51

It's wrong in WishListAdapter,

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

It will make recyclerView and adapter render no item.

It should be

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

Upvotes: 0

Related Questions