Skip to content
Advertisement

Firebase data not displaying in Recycler view. How do I display user specific data from firebase in recyclerview?

I have attached the firebase data image at the bottom for reference. I have a list of users. In that few users have a node called Cart. I need to display the data in the cart for that specific user.

Below is my Activity code

package com.shopping.grocery_ekart.Activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.shopping.grocery_ekart.Adapters.AdapterOrderUser;
import com.shopping.grocery_ekart.Adapters.AdapterProductUser;
import com.shopping.grocery_ekart.Adapters.CartAdapter;
import com.shopping.grocery_ekart.Models.CartModel;
import com.shopping.grocery_ekart.Models.ModelOrderUser;
import com.shopping.grocery_ekart.Models.ModelProduct;
import com.shopping.grocery_ekart.R;

import java.util.ArrayList;

public class CartActivity extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;
    private RecyclerView recycler_cart;

    private ArrayList<CartModel> cartList;
    private CartAdapter cartAdapter;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        
        firebaseAuth = FirebaseAuth.getInstance();

        recycler_cart = findViewById(R.id.recycler_cart);
        
        loadCartItems();
    }

    private void loadCartItems() {
        //init list
        //get orders
        cartList = new ArrayList<>();
        String timestamp = ""+System.currentTimeMillis();

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child("Cart");
        reference.orderByChild("uid").equalTo(firebaseAuth.getUid())

                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        //clear list before adding item
                        cartList.clear();
                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            CartModel cartModel = ds.getValue(CartModel.class);
                            cartList.add(cartModel);

                        }

                        //setup adapter
                        cartAdapter = new CartAdapter(CartActivity.this, cartList);
                        //set adapter
                        recycler_cart.setAdapter(cartAdapter);

                    }

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

                    }
                });

    }
}

Here is my adapter class

package com.shopping.grocery_ekart.Adapters;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.firebase.auth.FirebaseAuth;
import com.shopping.grocery_ekart.Models.CartModel;
import com.shopping.grocery_ekart.Models.ModelProduct;
import com.shopping.grocery_ekart.R;

import java.util.ArrayList;


public class CartAdapter extends RecyclerView.Adapter<CartAdapter.HolderCart> {

    private Context context;
    public ArrayList<CartModel> cartList;

    private FirebaseAuth firebaseAuth;

    public CartAdapter(Context context, ArrayList<CartModel> cartList) {
        this.context = context;
        this.cartList = cartList;

        firebaseAuth = FirebaseAuth.getInstance();
    }


    @NonNull
    @Override
    public HolderCart onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.cart_itmes, parent, false);
        return new HolderCart(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HolderCart holder, int position) {
        //get data
        CartModel cartModel = cartList.get(position);
        String addedBy = cartModel.getAddedBy();
        String itemId = cartModel.getItem_Id();
        String productId = cartModel.getItem_PID();
        String title = cartModel.getItem_Name();
        String priceEach = cartModel.getItem_Price_Each();
        String price = cartModel.getItem_Price();
        String quantity = cartModel.getItem_Quantity();
        String availableStock = cartModel.getAvailable_Stock();

        //set data
        holder.txtName.setText(title);
        holder.txtQuantity.setText(quantity);
        holder.txtPrice.setText("₹" + price);
        holder.priceEach.setText("₹" + priceEach);
        holder.availability.setText(availableStock);

    }

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

    class HolderCart extends RecyclerView.ViewHolder {

        private TextView txtName, txtQuantity, txtPrice, priceEach, availability;
        private ImageView imageView, btnMinus, btnPlus, btnDelete;

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

            //init ui views
            txtName = itemView.findViewById(R.id.txtName);
            txtQuantity = itemView.findViewById(R.id.txtQuantity);
            txtPrice = itemView.findViewById(R.id.txtPrice);
            priceEach = itemView.findViewById(R.id.priceEach);
            imageView = itemView.findViewById(R.id.imageView);
            btnMinus = itemView.findViewById(R.id.btnMinus);
            btnPlus = itemView.findViewById(R.id.btnPlus);
            btnDelete = itemView.findViewById(R.id.btnDelete);
            availability = itemView.findViewById(R.id.availability);

        }
    }

}

Here is my Model class

package com.shopping.grocery_ekart.Models;


public class CartModel {

    private String AddedBy, Item_Id, Item_PID, Item_Name, Item_Price_Each, Item_Price, Item_Quantity, Available_Stock;

    public CartModel() {

    }

    public CartModel(String addedBy, String itemId, String productId, String title, String priceEach,
                     String price, String quantity, String availableStock) {

        AddedBy = addedBy;
        Item_Id = itemId;
        Item_PID = productId;
        Item_Name = title;
        Item_Price_Each = priceEach;
        Item_Price = price;
        Item_Quantity = quantity;
        Available_Stock = availableStock;

    }

    public String getAddedBy() {
        return AddedBy;
    }

    public void setAddedBy(String addedBy) {
        AddedBy = addedBy;
    }

    public String getItem_Id() {
        return Item_Id;
    }

    public void setItem_Id(String item_Id) {
        Item_Id = item_Id;
    }

    public String getItem_PID() {
        return Item_PID;
    }

    public void setItem_PID(String item_PID) {
        Item_PID = item_PID;
    }

    public String getItem_Name() {
        return Item_Name;
    }

    public void setItem_Name(String item_Name) {
        Item_Name = item_Name;
    }

    public String getItem_Price_Each() {
        return Item_Price_Each;
    }

    public void setItem_Price_Each(String item_Price_Each) {
        Item_Price_Each = item_Price_Each;
    }

    public String getItem_Price() {
        return Item_Price;
    }

    public void setItem_Price(String item_Price) {
        Item_Price = item_Price;
    }

    public String getItem_Quantity() {
        return Item_Quantity;
    }

    public void setItem_Quantity(String item_Quantity) {
        Item_Quantity = item_Quantity;
    }

    public String getAvailable_Stock() {
        return Available_Stock;
    }

    public void setAvailable_Stock(String available_Stock) {
        Available_Stock = available_Stock;
    }
}

I have a list of users. In that few users have a node called Cart. I need to display the data in the cart for that specific user.

enter image description here

Advertisement

Answer

Add this code on CartActivity

recycler_cart.setLayoutManager(new LinearLayoutManager(getActivity()));

onCreate

Advertisement