问题
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 to list in Firebase database?
mRefUser.push().setValue(new MessageItem());
mRefUser.push().setValue(new MessageItem());
.
.
mRefUser.push().setValue(new MessageItem());
How is it getting added?
Firebase stores each object as key value pair in HashMap.
How am I retrieving these values?
mFireBaseRefnew.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) {
}
});
What am I getting?
Last Note :-
The hashmap keys are in random order. Is there any way to get linked hash map instead of hashmap. i dont want to store the keys since database is maintaining the order , there should be some way to get them in the same order but i dont know how to get them.
回答1:
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
回答2:
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.
回答3:
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) {
}
});
回答4:
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
来源:https://stackoverflow.com/questions/38615242/android-how-to-retrieve-list-of-objects-in-their-insertion-order-from-firebase