2

I'm working on the android app and I have a problem when trying to read products data from firebase. I need these product name, product price and product description data to use in recycler view.

When I tried to run the code I didn't get this data.
I tried the given code:

ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");

    ProductsRef.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            productItems = getAllItems(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w( "Failed to read value.", error.toException());
        }
    });


public  ArrayList<Products> getAllItems(DataSnapshot dataSnapshot){

  items  = new ArrayList<Products>();

  for (DataSnapshot item : dataSnapshot.getChildren()) {

    items.add(new Products(
            item.child("category").getValue().toString(),
            item.child("description").getValue().toString(),
            item.child("date").getValue().toString(),
            item.child("image").getValue().toString(),
            item.child("pid").getValue().toString(),
            item.child("pname").getValue().toString(),
            item.child("price").getValue().toString(),
            item.child("time").getValue().toString()
    ));

  }

  return items;
}
  • 1
    What is the problem? As in: when you run this code, which line doesn't do what you expect it to do? Note that it's often easiest to explain things if you add log statements, show what they print, and what you expected them to print. – Frank van Puffelen Sep 11 '19 at 01:37
  • 1
    If you put a breakpoint in `onDataChange` and run the code in a debugger, does it get triggered? If so, step through the code, and see what line doesn't work. My educated guess is that you're trying to access the data before it is loaded, but there's not enough information in your question to be sure. If that is the case, see https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Sep 11 '19 at 02:16
  • You cannot simply use `productItems` outside the `onDataChange()` method. Please check the duplicates to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Sep 11 '19 at 08:20

1 Answers1

0
ProductsRef.addListenerForSingleValueEvent(
        new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //Get map of Products in datasnapshot
                getAllItems((Map<String,Object>) dataSnapshot.getValue());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.w( "Failed to read value.", error.toException());
            }
        });

private ArrayList getAllItems(Map<String,Object> products) {

    ArrayList<Long> list = new ArrayList<>();
    for (Map.Entry<String, Object> entry : products.entrySet()){
        //Get user map
        Map value = (Map) entry.getValue();
        list .add((Long) value .get("category"));
        // add all your item
    }
}
Ashish
  • 6,791
  • 3
  • 26
  • 48
Suman Kumar Dash
  • 681
  • 5
  • 19