jo10
jo10

Reputation: 111

How can i get multiple keys from firebase

![enter image description here][1]

 mAdapter = new FirebaseRecyclerAdapter<Adapter_Hotels, Adapter_HotelsHolder>(mOptions) {
            @Override
            protected void onBindViewHolder(@NonNull final Adapter_HotelsHolder holder, int position, @NonNull Adapter_Hotels model) {

            String key = getRef(position).getKey();
            mRef1 = mRef.child(key).child("rooms");
            mRef1.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                        roomkey = child.getKey();
                    }
                    minmax.clear();
                    mRef2 = mRef1.child(roomkey).child("promos").child("regular").child(ym).child("price").child(guest);
                    mRef2.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                            }

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

        }

Here is all of my queries. Is this advisable? It's totally working I removed all the codes for retrieving. My question is any other way to do this nicer or cleaner?

Upvotes: 1

Views: 194

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599946

Your code looks a bit more complex than needed. As far as I can see you can accomplish the exact same with:

mRef1 = mRef.child(key).child("rooms");
mRef1.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot roomSnapshot: dataSnapshot.getChildren()) {
            roomkey = child.getKey();
            DataSnapshot priceSnapshot = roomSnapshot.child(roomkey).child("promos").child("regular").child(ym).child("price").child(guest);
            System.out.println(priceSnapshot.getValue());
        });
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

Changes:

  • Don't leave onCancelled empty, as undetected errors are really hard to troubleshoot.
  • Put the lookup of the price inside the loop, so that it looks up prices for all rooms. If you're only looking for the price of one room, you should use a query to only read that single room.
  • There is no need to use a separate listener for the price itself, as (as far as I can see) the price is already in the dataSnapshot for rooms.

Upvotes: 1

Mr. Patel
Mr. Patel

Reputation: 1385

Try this:

for (DataSnapshot child : snapshot.getChildren()) {
    Log.i("TAG", "child key = " + child.getKey());
}

Upvotes: 1

Related Questions