Retrieva Data from Firebase for RecyclerView

  android-studio, firebase

I create a recyclerView and i want to retrieve data from firebase and display it in recyclerview. I have create two models.
Check image for firebase
enter image description here

The order which contains id,price,productId,productName and quantity is from an another list.
I want for example setText in orderDetails the productName
I cannot take with the same way because is another list
My Request Model is :

public class Request {

    private String name;
    private String address;
    private String total;
    private String datetime;
    private String paymentMethod;
    private List<CoffeeOrder> order;


    public Request() {
    }

    public Request(String name, String address, String total, String datetime, String paymentMethod, List<CoffeeOrder> order) {
        this.name = name;
        this.address = address;
        this.total = total;
        this.datetime = datetime;
        this.paymentMethod = paymentMethod;
        this.order = order;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public String getDatetime() {
        return datetime;
    }

    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }

    public String getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    public List<CoffeeOrder> getOrder() {
        return order;
    }

    public void setOrder(List<CoffeeOrder> order) {
        this.order = order;
    }
}

My CoffeeOrder Model is:

public class CoffeeOrder {

    private int ID;
    private String ProductId;
    private String ProductName;
    private String Quantity;
    private String Price;

    public CoffeeOrder() {
    }


    public CoffeeOrder(int ID, String productId, String productName, String quantity, String price) {
        this.ID = ID;
        ProductId = productId;
        ProductName = productName;
        Quantity = quantity;
        Price = price;

    }


    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getProductId() {
        return ProductId;
    }

    public void setProductId(String productId) {
        ProductId = productId;
    }

    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String productName) {
        ProductName = productName;
    }

    public String getQuantity() {
        return Quantity;
    }

    public void setQuantity(String quantity) {
        Quantity = quantity;
    }

    public String getPrice() {
        return Price;
    }

    public void setPrice(String price) {
        Price = price;
    }
}

My adapter is:

public class UserOrderAdapter extends RecyclerView.Adapter<UserOrderAdapter.UserOrderViewHolder> {

    ArrayList<Request> myList;
    Context context;


    public UserOrderAdapter(Context context, ArrayList<Request> myList) {
        this.myList = myList;
        this.context = context;

    }

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

    @Override
    public void onBindViewHolder(@NonNull UserOrderViewHolder holder, int position) {

        Request request = myList.get(position);

        holder.usernameText.setText(request.getName());
        holder.orderTotalPrice.setText(request.getTotal());
        holder.orderDate.setText(request.getDatetime());
        holder.paymentMethod.setText(request.getPaymentMethod());



    }

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

    public static class UserOrderViewHolder extends RecyclerView.ViewHolder {

        TextView usernameText, orderTotalPrice, orderDate, paymentMethod, orderDetails;


        public UserOrderViewHolder(View itemView) {
            super(itemView);

            usernameText = (itemView).findViewById(R.id.username_text);
            orderTotalPrice = (itemView).findViewById(R.id.order_total_price);
            orderDate = (itemView).findViewById(R.id.order_date);
            paymentMethod = (itemView).findViewById(R.id.payment_method);
            orderDetails = (itemView).findViewById(R.id.order_details);


        }

    }

}

And my activity is:

public class UserOrderActivity extends AppCompatActivity {

    RecyclerView recyclerOrders;
    private FirebaseDatabase db = FirebaseDatabase.getInstance();
    private DatabaseReference ref = db.getReference().child("Requests");
    private UserOrderAdapter adapter;
    private ArrayList<Request> list;

    Toolbar toolbar;
    TextView userOrderBlankText;

    GifImageView gifImageView;


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

        toolbar = findViewById(R.id.toolbar);
        gifImageView = findViewById(R.id.order_giffy);
        userOrderBlankText = findViewById(R.id.user_order_blank_text);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        recyclerOrders = findViewById(R.id.recycler_orders);
        recyclerOrders.setHasFixedSize(true);
        recyclerOrders.setLayoutManager(new LinearLayoutManager(this));

        list = new ArrayList<>();
        adapter = new UserOrderAdapter(this, list);
        recyclerOrders.setAdapter(adapter);


        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                    Request request = dataSnapshot.getValue(Request.class);
                    list.add(request);
                }

                adapter.notifyDataSetChanged();

                if (list.size() == 0) {
                    gifImageView.setVisibility(GifImageView.VISIBLE);
                    userOrderBlankText.setVisibility(View.VISIBLE);
                }
            }

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

            }
        });

    }
}

Source: Android Studio Questions

LEAVE A COMMENT