0

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.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Igor Lerinc
  • 65
  • 10
  • So your question is if that query works? – Alex Mamo Nov 14 '19 at 11:11
  • yea, https://stackoverflow.com/questions/32526960/filter-products-based-on-the-sub-child-in-firebase This is what i am trying to accomplish, but will i be able to retrieve data feom parent child. Because i need to check for .equal in subchild. – Igor Lerinc Nov 14 '19 at 11:13
  • I'm afraid I didn't understand what you are saying. Please provide a concrete example. – Alex Mamo Nov 14 '19 at 11:25
  • i want to filter child data according to sub-child value. And when i filter it, how i can retrieve child's other values, like "availability", "email", "name" (look in picture above, my database structure). I am using FROM, PASSING and TO location sub-childs of postId, to filter data for displaying user posts related to his country where he curently is. So if he is in MNE, it will show him post. So i need a way to retrieve other data for post, like "email", "map" etc. But when i tell Query to look up to a path, it cant go back, so it will read just for .equal , but cant retrieve me other data. – Igor Lerinc Nov 14 '19 at 11:35
  • 1
    @IgorLerinc did you try the answer? – Peter Haddad Nov 15 '19 at 06:01
  • thanks for help, it helped me to understand how i can accomplish it. But issue with your answer is: that my app need to listens for 3 sub-childs (in trading info). I mean, if "TO location" doesnt contain "USA" then check "FROM location" node if it contains "USA", if "FROM location" doesnt contain "USA", then check " PASSING location" if it contains "USA" value. And in "volunteer services" it goes same, check if "FROM location" contains "USA", if not then check "REQUEST FROM" if it contains "USA". Exactly this is what i am trying to accomplish, like if, else statement. – Igor Lerinc Nov 15 '19 at 10:50
  • And i dont know how to make that statement, to check on multiple fields for value. I dont know, should i use multipme queries, or how to accomplish that. I readed that firebase doesnt support multiple queries at same time. So, i dont know how to make this statement i explained. – Igor Lerinc Nov 15 '19 at 10:51

1 Answers1

1

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.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • thanks for help, it helped me to understand how i can accomplish it. But issue with your answer is: that my app need to listens for 3 sub-childs (in trading info). I mean, if "TO location" doesnt contain "USA" then check "FROM location" node if it contains "USA", if "FROM location" doesnt contain "USA", then check " PASSING location" if it contains "USA" value. And in "volunteer services" it goes same, check if "FROM location" contains "USA", if not then check "REQUEST FROM" if it contains "USA". Exactly this is what i am trying to accomplish, like if, else statement. – Igor Lerinc Nov 15 '19 at 10:53
  • because in your code, it goes like this: if "TO location" contains "USA" then read other two sub-childs if they contain same. But i need behaviour i explained in comment. Is it accomplished adding multiple DatabaseReference methods to check for that? – Igor Lerinc Nov 15 '19 at 10:57
  • and yes, i forgot to mention, if any of those 3 sub-child contains "USA" ( in "trading info" ) or in "volunteer services" if those 2 sub-child contains "USA"; then to retrieve "availability", "date of traveling", "desription", "email", "map", "mode of travel", "name", "phone", "planned time coming at location", "publish date", "title". That's fully what i am trying to implement. – Igor Lerinc Nov 15 '19 at 11:16
  • Just in the first listener retrieve all the values then add a query and check if it is not null and attach a listener – Peter Haddad Nov 15 '19 at 11:16
  • And one side question: firebase will know to retrieve data from other child's where "USA" is found in sub-child? I mean, if he found in "postId" child , that a sub-child of "postId" contains "USA" ("TO location" contains "USA"), it will retrieve other childs (like "title", "name", etc) from that same "postId" child. Because i worked on MySQL and you need to do everything manual there – Igor Lerinc Nov 15 '19 at 11:20
  • firebase is not like mysql, here you can query using `equalTo()` and other queries: https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query. In the first listener in my answer retrieve `from Location` and `to location` and `passing` then after retrieving all the values.. – Peter Haddad Nov 15 '19 at 11:34
  • do `Query voluneterService = FirebaseDatabase.getInstance().getReference("Volunteer services").orderbyChild("FROM location").equals(fromLocation)` and check if is it not null then add a listener and retrieve all the values inside volunteer services, else if it is null then check if `request from` equal to a value and add a listener... – Peter Haddad Nov 15 '19 at 11:35
  • so basically retrieve everything in the first listener then create a query and add if...else if...else... and retrieve all the data in the second listener – Peter Haddad Nov 15 '19 at 11:37
  • yea, thanks. you opened my eyes when you said " then create a query and add if...else if...else... and retrieve all the data in the second listener ", so that way i can check for everything, and be able to retrieve all data. Tnx again. – Igor Lerinc Nov 15 '19 at 11:58