8

i want to get last child from my firebase data structure in which i only know the reference of received and first child of it enter image description here

i try this one but it will return all child but i need only last one that use in query limiTolast(1) like this

  DatabaseReference users = mDatabase.child("received");
      //  DatabaseReference receiver = users.child(firebaseUser.getUid());
        final DatabaseReference receiver = 
    users.child("GTjrWgpKjoeXUt4JdBJTYP1JkVT2");

        receiver.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                Log.d(TAG, "key count=" + postSnapshot.getKey());

                for (DataSnapshot sender: postSnapshot.getChildren()) {

                    Log.d(TAG, "sender key count=" + sender.getKey());

                }

            }


        }


        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("SHAN " ,databaseError.getMessage());
        }
    });
Muhammad Shan
  • 226
  • 1
  • 3
  • 7
  • vist [how to ask](http://stackoverflow.com/help/how-to-ask) – Mathews Sunny Apr 22 '17 at 08:44
  • Read in the entire node fnB0M (by value), add the elements to an array. The last item in the array is the one you want. Or query that node for a child contained in the last element. Or, add a child to each node; isLast: false and make it isLast: true for the last one, then query by that. Or, as you mentioned use limitToLast - making sure to read in the correct parent node, which would be the fnB0M one, not the GTjr node. – Jay Apr 23 '17 at 11:46

5 Answers5

11

If you know the complete path of the parent node (received/GTjrWgpKjoeXUt4JdBJTYP1JkVT2/fnBOM...), you can get only the last child node under that location with limitToLast(1):

DatabaseReference ref = users.child("GTjrWgpKjoeXUt4JdBJTYP1JkVT2/fnBOM...`");
ref.orderByKey().limitToLast(1).addChildEventListener(...

If you don't know the complete path to the parent node, there is no way to retrieve a subset of the child nodes.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
4

You can use a query with the 'limitToLast' method that returns a specified number of items beginning from the last.

Query query=datasnapshot.child("childname").orderByKey().limitToLast(1);

2

It is a best solution to find the last exit child from firebase

 String LASTMESSAGE=NULL; DatabaseReference dbref =
 Firebase.getInstance().getReference(); Query lastchield =
 FirebaseDatabase.child("yourchieldfororder")
                           .orderByKey().limitToLast(1); lastchield.addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot ds) {
          LASTMESSAGE= ds.child("message").getValue().toString();
     }
     @Override
     public void onCancelled(DatabaseError databaseError) {
         LASTMESSAGE = "No-Last-MESSAGE-EXIT";
     } });`

Query first order the child id then give you the last id of the child in data- snapshot.

Happy Coding!

equasezy
  • 45
  • 1
  • 7
Sohaib Aslam
  • 1,245
  • 17
  • 27
0

Your reference is not correct your receiver databaseReference should point to fnBOM..... instead of "GTjrWgpKjoeXUt4JdBJTYP1JkVT2"

       final DatabaseReference receiver = 
       users.child("GTjrWgpKjoeXUt4JdBJTYP1JkVT2").child("fnBom...");

and your can store each child on an arraylist and the access last element from arraylist by

        myArrayList.get(myArrayList.size()-1);
0

finally i make the logic for getting last node even we don't know the exact reference of our child

solution

In my scenario there are three parent

  1. received (The root node)
  2. the user GTjr..... ( i just know the reference only this point )
  3. and the third node fnBom... (actually problem in this i need to get last child of this array )

so i get the total ChilderenCount of (fnBom.. node) in my case it is 5 , now i used for loop and match it with if condition to get last nod by this

if(loopCount==childrenCount){

 // here we can get the last child 

}

thanks for all of you people :)

Muhammad Shan
  • 226
  • 1
  • 3
  • 7
  • This is bad solution ever, really you get all the list then you choose one of them.. You should change your database structure. – tabebqena Aug 13 '18 at 02:08