Get the array elements of the last node in Firebas


As shown in the image below, I have a database "table" called fridge that has a child called food. food is an array that can contain one or more elements. I want to access the last node and fetch the food elements and add them in a list, but I couldn't figure out how to do it.

Thank you for your help


You could follow the docs and use the limitToLast() method. Keys in firebase are ordered alphabetically.


You can try this to get the data:

mDatabase.child("fridge").child(fridgeId).child("food").addListenerForSingleValueEvent( new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { String foodItem = postSnapshot.getValue(); foodList.add(foodItem); } } });


First, create a list to store those values.

List<String> food = new ArrayList<>();

Retrieve the last stored food inside each fridge key

mDatabase.child("fridge").child(yourPushID).child("food").limitToLast(1).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot snapshot : dataSnapshot.getChildren()){ String lastFood = snapshot.getValue(String.class); //Add your food to the list food.add(lastFood); Log.e("Foods found:",""+lastFood); } } @Override public void onCancelled(DatabaseError databaseError) { System.out.println("The read failed: " + databaseError.getCode()); } });

Where yourPushID should be how you are generating those random keys to store your food

Where mDatabase is

DatabaseReference mDatabase; mDatabase = FirebaseDatabase.getInstance().getReference();


  • How to automatically delete a user in Firebase?
  • FireBase Query. DataSnapshot
  • Change values in Firebase database nodes by a given criteria
  • Android::Data are not retrieving to EditText field through Database
  • Trouble with viewbinder Simplecursortreeadapter -unable to convert string to blob
  • Use the contents of a file to replace a string using SED
  • Warning - Initialization makes pointer from integer without a cast
  • CountDownTimer trivia game score - Android (java)
  • java.io.IOException: open failed: EACCES (Permission denied) when writing file in API level 23
  • Firebase : How to read every child node in parent except children with particular data
  • Properties Order while Deserialization using JSON.Net
  • PHP ksort seems unaffected by setlocale
  • Connect a prototype cell with a plist in xcode 4
  • Passing multi parameters (radio) from view to controller using array
  • Get longer time zone name in PHP
  • OpenMP for dependent variables
  • Rails 4 order by virtual attribute
  • R Leaflet Legend: specify order instead of alphabetical
  • Firebase, only get new children
  • Detect language of Word document
  • What is the equivalent of Android permissions in iOS development? [duplicate]
  • Loop through each key and value of php multidimensional array
  • Many to Many in Linq using Dapper
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • netsh acl setting (need alternative method - registry settings?)
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • Array with custom indexes in Ionic2
  • Check if a string to interpolate provides expected placeholders
  • Trying to switch camera back to front but getting exception
  • Javascript + PHP Encryption with pidCrypt
  • Rearranging Cells in UITableView Bug & Saving Changes
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • embed rChart in Markdown
  • Programmatically clearing map cache
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app