What do I want?
I want to retrieve the list of objects in their insertion order from Firebase Database.
How am I adding the object
Have you read documentation? Data keys is timestamp based, so they ordered by time. To get sorted query you must use orderByKey()
on database reference. More about it https://firebase.google.com/docs/database/android/retrieve-data#sorting_and_filtering_data
To retrieve sorted data, start by specifying one of the order-by method. For more https://firebase.google.com/docs/database/android/lists-of-data
If you are having HashMap inside class and want to get whole firebase object using getValue(Classname.class)
Method bellow will help you
get it into TreeMap from HashMap and you'll have ordered list as in firebase by key
Example code
TreeMap<String,Recordings> recordingsTreeMap = new TreeMap<>();
recordingsTreeMap.putAll(project.getRecordings());
You will have ordered list by key in TreeMap object.
where project is a class having multiple fields and one or more HashMap and you don't want to query each separately.
Here is an example to illustrate Mr. Dima Rostopira response more clearly. Do you see the "orderByKey()"?
mFireBaseRefnew.orderByKey().addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final Map<String, MessageItem> messageMap = new LinkedHashMap<String, MessageItem>();
if (dataSnapshot != null && dataSnapshot.getValue() != null) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
HashMap<String,MessageItem> messageMap = (HashMap<String, MessageItem>) postSnapshot.getValue();
Collection<MessageItem> messageItems = messageMap.values() ;
List<MessageItem> messageItemList = new ArrayList<MessageItem>();
messageItemList.addAll(messageItems);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});