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;
}
3
  • 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. Commented Sep 11, 2019 at 1: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 stackoverflow.com/questions/50434836/… Commented Sep 11, 2019 at 2: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. Commented Sep 11, 2019 at 8:20

1 Answer 1

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.