1

I want to retrieve data from Firebase according to creation date and time I haven't found any other method instead of creating a child of every user to save creation date and time sort by using orderByChild("Create") but every user is saved with a random id how can I point to sort by the child of:

DatabaseReference userref = FirebaseDatabase.getInstance().getReference().child("Connection").child("Admin");
        userref.orderByChild("Create");

        userref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {

                        Users users = userSnapshot.getValue(Users.class);
                        adminsList.add(users);

                    }
                    mAdapter = new Adapter_All_Admins(adminsList);
                    mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
                    mRecyclerView.setAdapter(mAdapter);


                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


Firebase Users Data Image

Community
  • 1
  • 1
  • It is really hard for me to understand your exact problem by reading your question. I am unable to figure out what your ultimate goal is? What are your expected and current outputs? Why are you concerned about random keys? what did you mean by, "how can I point to sort by the child of" ? Lastly, as you already know, by using orderByChild("Create") you can sort it, so what is the issue? – Roaim Sep 11 '19 at 07:49
  • actually orderByChild("Create") will order userref as userref reffers to "Admin" but there is no child in Admin like "Create" it is a child of random key that contains user data i want the to way that reffers to random key so orderByChild("Create") can work – Muhammad Owais Chaudhary Sep 11 '19 at 07:56

1 Answers1

2

There are more than a single problem in your code.

I haven't found any other method instead of creating a child of every user to save creation date and time sort by using orderByChild("Create")

That's the recommended approach to create a separate node for every user. First problem is that Firebase realtime database queries are immutable, which means that you cannot change the properties of an existing query. If you change the value by calling .orderByChild("Create") method, it becomes a new query. That's the exact same behaviour as in case of the String class. So to solve this, please chain all method calls and store them in a single Query object:

Query userref = FirebaseDatabase.getInstance().getReference()
    .child("Connection")
    .child("Admin")
    .orderByChild("Create");

Or you can create a new Query object like this:

Query createQuery = userref.orderByChild("Create");
createQuery.addValueEventListener(/* ... */);

Second problem, to have relevant results, please note that the timestamp should not be stored as a String:

Create: "2019/09/11 02.32:54"

Because the order in this case would be lexicographically. So you definitely should store them as a ServerValue.TIMESTAMP, as explained in my answer from the following post:

The third problem is that the properties in your database are starting with a capital letter. If the fields in your Users class are lowercase or even if are starting with a capital letter, you might get the following error:

W/ClassMapper: No setter/field for Create found on class Users

To solve this, you should either use the answer from the following post:

Or from the following post:

Edit:

When you query, there is no need to add the uid 0yozyNJzCvTWtRU8ufe5Zx8TPvu1 as child. You should only get a reference to Admin and then loop through the children (which are actually user object). When you call .orderByChild("Create") you are transforming the DatabaseReference object into a Query object and you are ordering all users according to the Create property.

You should add an explicit call to child("0yozyNJzCvTWtRU8ufe5Zx8TPvu1") only if you want to get a particular user obect, otherwise you don't need to do it.

or you're .orderByChild() can work for sub child too ??

Yes, it will work. Give it a try ;)

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • but here .child("Admin") .orderByChild("Create"); there is not child as "Create" it is sub child of "Admin" in between "Admin" and "Create" there is a random key using i can say '.child("Admin").child("0yozyNJzCvTWtRU8ufe5Zx8TPvu1") .orderByChild("Create"); 'but i'm not retrieving data of a single key so i can use this what i can use in place of this random key so i can sort all the data according to "Create" in every random key ,,,, or you're .orderByChild() can work for sub child too ?? – Muhammad Owais Chaudhary Sep 11 '19 at 09:10
  • it's working fine can you add me a favor to sort it ascending or descending live if a user was created on 2019 and second on 2020 it 2020 user should at the bottom and 2019 user on top – Muhammad Owais Chaudhary Sep 11 '19 at 11:51
  • 1
    This question is not related with the initial one. So in order to follow the rules of this community, please post another fresh question, so me and other Firebase developers can help you. – Alex Mamo Sep 11 '19 at 12:11
  • i think no one can handle firbase question better then you please have a look here [Here is my question ](https://stackoverflow.com/questions/57912265/when-data-is-changed-in-real-time-databasefirebase-app-crashes) – Muhammad Owais Chaudhary Sep 13 '19 at 05:14