0

I am making an Android app in Android Studio and I want to get specific data from the Firebase. I am trying to search through the Firebase Realtime Database to get all of the Users that have a usercode of 1234 and then getting their name. This is how I have it set up in Firebase:

Firebase Setup image link

Since I am searching for the data where the usercode = 1234 it will return (in the datasnapshot) all of the information about Joe and Lily (since they were the two users that had a usercode of 1234), but it shouldn't return information about Kevin. Here is how I did that:

Query getUsers = ref.orderByChild("usercode").equalTo(1234);
    getUsers.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                //User with usercode = 1234 exists
            } else {
                //User with that Usercode doesn't exist
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

The datasnapshot, if converted to a string would have all of Lily and Joe's information I believe. What I want to do is get the first user's name (Which would be Joe in this case) from that datasnaphot. My code will then use it through a few tests before going to the second user's name(Which would be Lily) and repeating the tests. Also, if there were 100 Users then I would need to repeat it for every one of those 100 that had a user code of 1234, so I probably need something repeatable.

So somehow I need to find the first value of the child's name from all of the Users that have a usercode of 1234 and then repeat until I get all of the names and run the test on all of them.

I am new to Stackoverflow, so please tell me if I am missing any information that would be helpful in answering the question.

---- EDIT ----

I have now figured out how to do this with Firebase Firestore. It has simpler queries and allows you to do more with the results. If anyone else has the same issue that I stated above, Firebase Firestore is something you might want to try out.

Olivia
  • 1
  • 4

1 Answers1

0

When using the following line of code:

Query getUsers = ref.orderByChild("usercode").equalTo("1234");

You are searching through the Users node, all users that have the usercode property set to "1234". This query will always return no results because you are searching for 1234 as a String and not as a number, as it is stored in the database. The correct query should be:

Query getUsers = ref.orderByChild("usercode").equalTo(1234); //No double quotes
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • The question I have is that once I get this information from the database, how do I use it? That is what I am trying to figure out in the second half of my question, how do I use the retrieved information and get the first result, then the second result, and so on. Thank you for your answer though, and I'll be sure to fix that in my code. – Olivia Jul 30 '20 at 16:52
  • Any code that needs data from the database, needs to be inside the onComplete method, or be called from there. So that's the only way you can use that or see my answer from this **[post](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)**. Furthermore, to be able to get the first, second and so on, you should definitely iterate through the `DataSnapshot` object. – Alex Mamo Jul 31 '20 at 08:27