David.Warwick
David.Warwick

Reputation: 750

Android Firebase OrderBy

I am having a hard time with figuring out how to query my Firebase database. Here is what it looks like.

Firebase Database

And here is my code:

//RETRIEVE
    public ArrayList<Spacecraft> retrieve()
    {

        String myUserId = acct.getId();

        //db.addChildEventListener(new ChildEventListener() {
        db.child("/users/uid").equals(myUserId)
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                //fetchData(dataSnapshot);
                fetchData(dataSnapshot);
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                fetchData(dataSnapshot);
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }
            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
        return spacecrafts;
    }

So db.addChildEventListener will retrieve the entire database. But what I want is to only retrieve data for users whose uid is equal to String myUserId. And I want to sort in ascending order by Level. I have read the docs and watched videos but I cannot figure it out. Any help would be appreciated.

Upvotes: 0

Views: 4448

Answers (2)

Fitsum Alemu
Fitsum Alemu

Reputation: 409

You can reverse the array list

    ArrayList<UserModel> user_list = new ArrayList<>();       

 for (DataSnapshot snapshot :dataSnapshot.getChildren()) {

                UserModel userModel = snapshot.getValue(UserModel.class);

                user_list.add(userModel);
                Collections.reverse(user_list);

            }

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599956

Query query = db.child("users").orderByChild("uid").equalTo("myUserId");
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot userSnapshot: snapshot.getChildren()) {
            System.out.println("User "+userSnapshot.child("uid").getValue());
        }
    }
    ...

But if you're frequently accessing the data by UID, you're off restructuring your database to store all users under their own UID:

users
  myUserId
    Level: 2
    NumCorrect: 8

You can then read the data with:

db.child("users/myUserId").addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot snapshot) {
        fetchData(dataSnapshot);
    }

For more on Firebase queries, see the Firebase documentation on sorting and filtering data. Since you're new to NoSQL, I also recommend reading NoSQL data modeling and viewing Firebase for SQL developers.

Upvotes: 3

Related Questions