how can i point to the random key( firebase user ID)

Muhammad Owais Chaudhary :

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

Alex Mamo :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328338&siteId=1