0

This is my structure Firebase database. I need get all order details (orderdate, ordername etc) to arraylist. But I don't know how much orders (order1, order2...) will be.

database strukture

This is my model class:

public class OrderDetails {
String orderdate,ordername;

public OrderDetails() {
}

public OrderDetails(String orderdate, String ordername) {
    this.orderdate = orderdate;
    this.ordername = ordername;
}

public String getOrderdate() {
    return orderdate;
}

public void setOrderdate(String orderdate) {
    this.orderdate = orderdate;
}

public String getOrdername() {
    return ordername;
}

public void setOrdername(String ordername) {
    this.ordername = ordername;
} }

This is my code:

 ref = FirebaseDatabase.getInstance().getReference().child("Customers").child(cityID).child(customerID).child("orders");

List<OrderDetails> orderList = new ArrayList<>();

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

    for( DataSnapshot postSnapshot : dataSnapshot.getChildren()){
        OrderDetails details = postSnapshot.getValue(OrderDetails.class);
        orderList.add(details);
        toast.makeText(getContext(), orderList, Toast.LENGTH_SHORT).show();
    }
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

} });

But this code not working. Need help please!

janedtj
  • 3
  • 3

1 Answers1

0

The problem in your code is that your are passing as the second argument in your makeText() method a List object instead of a String. The following message that you get:

[com.example.janedtj.shopapk.models.OrderDetails@8a15b85, com.example.janedtj.shopapk.models.OrderDetails@474adda]

Is in fact what toString() method returns. It represents the address of the List object from the memory. If you want to display for example, what your order name property holds, please change the following line of code:

Toast.makeText(getContext(), orderList, Toast.LENGTH_SHORT).show();

to

Toast.makeText(getContext(), details.getOrdername(), Toast.LENGTH_SHORT).show();

Please also note, if you want to get the size of the list, the following lines of code is required:

for( DataSnapshot postSnapshot : dataSnapshot.getChildren()){
    OrderDetails details = postSnapshot.getValue(OrderDetails.class);
    orderList.add(details);
}
Toast.makeText(getContext(), String.valueOf(orderList.size()), Toast.LENGTH_SHORT).show();

And should be placed outside the for loop as you can see.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193