0

This is My dummy firebase structure.

 { "Restaurant":{
    pushId: {
            "Node A":{
                    0:{ id = 1234
                        // data of some object type
                      }
                    1:{ id = 2345
                      // data of some object type
                      }
                    2:{ id = 3456
                      // data of some object type
                      }
                 }
            "Node B":{
                    0:{ id = 6789
                        // data of some object type
                      }
                    1:{ id = 9876
                      // data of some object type
                      }
                    2:{ id = 0000
                      // data of some object type
                      }
                 }
        }

    }
}

I want to extract data from "Node A" and "Node B" at same time with some unique id(suppose id = 1234 & 0000). "Node A" and "Node B" contains same kind of object. I tried all the information I have but could get it working.

I am attaching code which I am have written. Following code is for real data.

ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot dataSnapshotChildren : dataSnapshot.getChildren()) {
                final DatabaseReference reference = dataSnapshotChildren.child(pref.getString("user_uid", null)).getRef();
                for (int j = 0; j < stringArray.length; j++) {
                    Log.d("res_details activity", "stringArray: " + stringArray[j]);
                    reference.orderByChild("id").equalTo(stringArray[j]).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Log.d("res_details activity", "dataSnapshot: " + dataSnapshot.getValue());
                            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                                Restaurant restaurant = singleSnapshot.getValue(Restaurant.class);
                                Log.d("res_details activity", "restaurant: " + restaurant);
                                restaurantArrayList.add(restaurant);
                            }
                            Log.d("res_details activity", "restaurantArrayList: " + restaurantArrayList.size());
                            setValue(restaurantArrayList);
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                            Log.d("res_details activity", "databaseError: " + databaseError);

                        }
                    });

                }
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

All the Database references are correct. I have checked with log statement. Above code only gives me only one result at a time. Please help me modify this code get it working. Thanks in advance. Any suggestion is appreciated.

P.S - I couldn't attach screenshot of my real firebase structure as stackoverflow wont let me.

Update New snippet:

  ref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.d("res_details activity", "s: " + s);
            for (DataSnapshot dataSnapshotChildren : dataSnapshot.getChildren()) {

                for (int j = 0; j < stringArray.length; j++) {
                    Log.d("res_details activity", "reference: " + dataSnapshotChildren.getRef().toString());
                    Log.d("res_details activity", "stringArray: " + stringArray[j]);
                    databaseReference.orderByChild("id").equalTo(stringArray[j]).addChildEventListener(new ChildEventListener() {
                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                            Log.d("res_details activity", "dataSnapshot: " + dataSnapshot.getValue());
                            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                                Restaurant restaurant = singleSnapshot.getValue(Restaurant.class);
                                Log.d("res_details activity", "restaurant: " + restaurant);
                                restaurantArrayList.add(restaurant);
                            }
                            Log.d("res_details activity", "restaurantArrayList: " + restaurantArrayList.size());
                            setValue(restaurantArrayList);
                        }


                        @Override
                        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                        }
....

I have tried childEventListener too. Now its not even triggering nested childEventListener. And I am not getting any result now. all references are correct. Query is correct. I dont understand why snippet is not retrieving data. Totally confuse now.

Raj krishna
  • 63
  • 10
  • In this case ValueEventListener isn't appropriate one to be used. ValueEventListener.onDataChange() is always being called once and last on a certain database reference. You'd be better off retrieved your data via ChildEventListener. http://stackoverflow.com/questions/34530566/find-out-if-child-event-listener-on-firebase-completely-load-all-data – Taras Mykhalchuk Jan 26 '17 at 06:37
  • @Taras I have tried with `childEventListener` too. But its not working. What's wrong in code.? – Raj krishna Jan 26 '17 at 10:52

1 Answers1

1

Your snapshot contains HashMap object. Node A and Node B are gonna be represented as keys with corresponding values (Object). Take a look at the example with a data structure like yours.

ref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()){
            HashMap<String, Long> m = (HashMap<String, Long>) snapshot.getValue();
            Log.d("test", String.valueOf(m.get("id")));
        }
    }
});

Please configure your filter criteria and you are good to go!

Taras Mykhalchuk
  • 829
  • 1
  • 9
  • 20
  • My firebase data contains custom class object "Restaurant". It has several variables and "id" uniquely identifies the objects of Restaurant type. According to my understanding you are retrieving value of ID from Hashmap. But thats not my requirement. Suppose I have couple of IDs, and using those IDs I want to retrieve data from firebase. Main issue is that those IDs could be in any node or both. For eg: (from snapshot) if i have 3 IDs - `1234`, `6789`, `0000`. Now i want to retrieve data of corresponding nodes with those IDs. Let me know if u require more info. – Raj krishna Jan 26 '17 at 12:33