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:
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.