0

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.

AL.
  • 36,815
  • 10
  • 142
  • 281
David.Warwick
  • 620
  • 1
  • 9
  • 28
  • 1
    Did you try using OrderBy child and then filtering it out on query like mentioned in Docs https://firebase.google.com/docs/reference/js/firebase.database.Query – Renu Yadav Feb 13 '17 at 02:47
  • Hi. Thank you for your response. Yes, I have looked at that documentation. The problem with the documentation is that they don't show how the dinosaur database is structured. If I posted a question about problems I am having with my database and didn't show the structure, someone would ask for me to show the structure. I don't know how to get a reference to users with a specific uid and order by what appears to be a sibling , Level. I have a SQL background and don't understand NoSQL. – David.Warwick Feb 13 '17 at 02:58

2 Answers2

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks Frank. I can see from your query how that would select a certain uid. But would it ensure that the results would be sorted in ascending order by Level? – David.Warwick Feb 13 '17 at 03:19
  • A Firebase query can only order/filter on one property. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Feb 13 '17 at 03:48
  • Ok, maybe I can load the snapshot results into a list and then sort the list. Then send the list to the Custom Adapter for the ListView. So new to Android. I am a C# SQL Server developer by day. This would be so much easier if Android used C# instead of Java. – David.Warwick Feb 13 '17 at 04:57
0

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);

            }
Fitsum Alemu
  • 359
  • 3
  • 2