本文介绍了Android的 - 如何从Firebase的插入顺序检索对象的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想从Firebase数据库中按照其插入顺序检索对象列表。 b $ b 我如何将对象添加到Firebase数据库中列出? mRefUser.push()。 setValue(new MessageItem()); mRefUser.push()。setValue(new MessageItem()); mRefUser.push()。setValue(new MessageItem()); p> 如何添加? $ b Firebase将每个对象存储为HashMap中的键值对。 我如何取回这些值? mFireBaseRefnew.addValueEventListener(new ValueEventListener(){ @Override public void onDataChange(D ataSnapshot dataSnapshot){最终映射< String,MessageItem> messageMap = new LinkedHashMap< String,MessageItem>(); (DataSnapshot postSnapshot:dataSnapshot.getChildren()){ HashMap< if(dataSnapshot!= null& amp; dataSnapshot.getValue()!= null){ 串,MessageItem> messageMap =(HashMap< String,MessageItem>)postSnapshot.getValue(); 集合< MessageItem> messageItems = messageMap.values(); List< MessageItem> messageItemList = new ArrayList< MessageItem>(); messageItemList.addAll(messageItems); $ b @Override public void onCancelled(FirebaseError firebaseError){ }); 我得到了什么? $ b 最后注意: 散列表键值是随机的。有没有办法获得链接哈希映射而不是哈希映射。我不想存储密钥,因为数据库维护顺序,应该有一些方法让他们在相同的顺序,但我不知道如何让他们。 解决方案你读过文档吗?数据键是基于时间戳的,所以他们按时间排序。要获得排序的查询,您必须在数据库引用上使用 orderByKey()。详细了解 https://firebase.google.com/docs/database/ android / retrieve-data#sorting_and_filtering_data What i want ?I want to retrieve the list of objects in their insertion order from Firebase Database.How i am adding the object to list in Firebase database ?mRefUser.push().setValue(new MessageItem());mRefUser.push().setValue(new MessageItem());..mRefUser.push().setValue(new MessageItem());How its getting added ?Firebase stores each object as key value pair in HashMap.How i am retrieving these value ? 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 i am 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. 解决方案 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 这篇关于Android的 - 如何从Firebase的插入顺序检索对象的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-05 10:46