Igor Lerinc
Igor Lerinc

Reputation: 65

filter child data in firebase

I want to populate recyclerview so i can use it for displaying posts.

This is how i populate recycler view: How can I retrieve data from Firebase to my adapter

my database structure

But depending on which country user is, i need to filter posts related to just his country.

In database structure, TO, FROM, PASSING, REQUEST FROM are what i need to filter. Because in those parameters i put country code, so i can filter just country where user is.

Can i just add this in query, and will i be able to retrieve other information from child, so i can display it in post when user click on post in recyclerview.

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();             Query query = rootRef.child("Trading info").orderbyChild("TO location").equals(USA);

So if child contains USA, it will be showed in post, and when user click on post, will i be able to retrieve all data as i would normally.

Sorry if not best explained. I want to know, will i be able to retrieve all other child nodes from filtered child nodes. To retrieve nodes from child node which contains USA in their child nodes.


And can use multiple queries, because i need to check FROM, PASSING, TO and REQUEST FROM location child nodes, if it contains country code i need.

Upvotes: 0

Views: 564

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

To retrieve email, name and availability try the following:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Trading info");
databaseReference.orderbyChild("TO location").equals("USA").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
         for(DataSnapshot ds : dataSnapshot.getChildren()){
           String fromLocation    = ds.child("FROM location").getValue(String.class);
           String passingLocation = ds.child("PASSING location").getValue(String.class);

              DatabaseReference voluneterService = FirebaseDatabase.getInstance().getReference("Volunteer services");
              voluneterService.orderbyChild("FROM location").equals(fromLocation).addValueEventListener(new ValueEventListener() {
                   @Override
               public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot ds : dataSnapshot.getChildren()){
                        String fromLocation    = ds.child("availability").getValue(String.class);
                        String passingLocation = ds.child("email").getValue(String.class);
                        }
                     }
                    @Override
              public void onCancelled(DatabaseError databaseError) {
                     throw databaseError.toException();
                   }
                });
          }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

First add reference at node Trading info then use orderByChild on attribute TO location and retrieve the FROM location and PASSING location.

Then add another reference to node Volunteer services and use orderByChild on attribute FROM location. Inside equalTo() use the variable fromLocation that you retrieve in the first listener, then you will be able to retrieve email, name and availibility.

Upvotes: 1

Related Questions