0
FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        usersList.clear();

        for (DataSnapshot ds: snapshot.getChildren()){
            ModelUsers modelUsers = ds.getValue(ModelUsers.class);
            

            if (!modelUsers.getyearSection().equals(fUser.getUid())){
                usersList.add(modelUsers);
            }

            adapterUsers = new AdapterUsers(getActivity(), usersList);

            recyclerView.setAdapter(adapterUsers);
        }
    }

This is my Firebase Database Users


{
  "Users": {
    "7xWet0t6jZYcajSpt70nqbQy29q1": {
      "Password": "randomber16",
      "email": "klmartisano@gmail.com",
      "fullName": "Kenneth L. Martisano",
      "uid": "7xWet0t6jZYcajSpt70nqbQy29q1",
      "usertype": "Student",
      "week5score": "4",
      "yearSection": "4-2"
    },
    "aMx87W9b0vaQTwocAqKzuEvGj0G3": {
      "Password": "randomber16",
      "email": "srosarda@gmail.com",
      "fullName": "Sean Harvey C. Rosarda",
      "uid": "aMx87W9b0vaQTwocAqKzuEvGj0G3",
      "usertype": "Instructor",
      "yearSection": "4-2"
    },
    "q78J0so5mgdIBlTfMRSxWqXuxPj1": {
      "Password": "randomber16",
      "email": "cambroneroanne09@gmail.com",
      "fullName": "Anne Cambronero",
      "uid": "q78J0so5mgdIBlTfMRSxWqXuxPj1",
      "usertype": "Student",
      "yearSection": "4-2"
    }
  },

im new to android studio and im asking if there's a way to this to diplay users info using child value "yearSection" and current user "yearSection"

Im trying to call users in firebase database using child value of "yearSection" and display users with specific yearSection that equals to the current users "yearSection"

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Is there an error message with your current code? Or does it just not do what you want it to do? If you're having an issue reading the data, please edit your question (there's a link right under it) to also show the data that you're trying to read (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jan 22 '23 at 03:04
  • hope to get help from everyone – Sean Rosarda Jan 22 '23 at 03:30
  • You need to use firebase query method, instead of event listener – tanni tanna Jan 22 '23 at 04:48
  • see https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – tanni tanna Jan 22 '23 at 04:55

1 Answers1

0

It sounds like you're trying to order and filter data, which would look something like this for your data structure:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");

Query query = ref.orderByChild("yearSection").equalTo("4-2");

query.addValueEventListener(new ValueEventListener() {
  ...

Of course you'll have to make sure you pass the correct value to equalTo for your use-case.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • if i log in as one of the user there and the yearsection is 4-2 of the current user then what i mean is how can i display the users with the same yearsection with the current user because there will be more user will be added but different yearsection value. and if login as 1-1 as a yearsection then i want to also display users with 1-1 yearsection. – Sean Rosarda Jan 22 '23 at 05:15
  • First off: does what I explained in my answer about how you will need to [sort and filter the data](https://stackoverflow.com/questions/70551039/getting-highest-score-values-from-firebase-database) make sense, and did you read the linked documentation? --- If so, if you don't already know the `"4-2"` value in my code; There's no way in Firebase queries to dynamically refer to another place in the database to look up a value.. So you'll need to laod the data from the current user, read it from there, and pass it into the `equalTo` call. – Frank van Puffelen Jan 22 '23 at 06:12