0

I am trying to pull the data from my firebase real time database. The problem i am facing here is that I can pull the data from /ItemList successfully but when I try to pull the data from /ItemRequest the data cant be pulled but when I give it a specific path such as /ItemRequest/admin it works. What I am trying to do is to populate a listview with all the child from /ItemRequest from both admin and user.I am using firebase list adapter to populate my listview so are there any ways to do what i want?

Database Layout

enter image description here

KENdi
  • 7,576
  • 2
  • 16
  • 31
Blacky_99
  • 155
  • 4
  • 20

2 Answers2

2

What I am trying to do is to populate a listview with all the child from /ItemRequest from both admin and user.

To solve this, you need to iterate over the DataSnapshot object twice, like in the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference itemRequestRef = rootRef.child("ItemRequest");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
            for(DataSnapshot ds : dSnapshot.getChildren()) {
                String productName = ds.child("ProductName").getValue(String.class);
                Log.d(TAG, productName);
            }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
itemRequestRef.addListenerForSingleValueEvent(valueEventListener);

The output in your logcat will be:

Manix
Manix
Manix

If you want to display those results in a ListView, please see my answer from this post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • My listview doesn't seem to be updating in real time when I make changes to the data in the database. Any idea why this occurs? – Blacky_99 Jan 17 '19 at 10:21
  • There might be many reasons for that behaviour so in order to follow the rules of this comunity, please post another fresh question with its own [MCVE](https://stackoverflow.com/help/mcve), so me and other Firebase developers can help you. – Alex Mamo Jan 17 '19 at 10:23
  • Hereby I have attached the link for the question ( https://stackoverflow.com/q/54234045/9045445 ) your help is really appreciated. Thank you from the bottom of my heart. – Blacky_99 Jan 17 '19 at 10:39
0

AFAIK, FirebaseListAdapter does not work well with child objects that have multiple childs themselves and there is no query method for you to filter them out. I would rather manually pull the child items one by one using DataSnapshots adding them into an arraylist then populating the list using a custom array adapter instead

boom
  • 184
  • 1
  • 9