本文介绍了在负面时间戳中按日期排序在Firebase中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用的查询: q = FirebaseDatabase.getInstance()。getReference()。child(products)。orderByChild ( 时间戳); q.addValueEventListener(new ValueEventListener(){ @Override public void onCancelled(DatabaseError databaseError){} @Override public void onDataChange(DataSnapshot dataSnapshot){ Log.d(datasnapshot,dataSnapshot.toString()); 这个值在我的Firebase数据库中的顺序相同。 注意:我使用负值方法 {-1 *新的Date().getTime();} 来存储时间戳。 这个值在我的Firebase数据库中的顺序相同: Ke7K4vXyEt4gKnjf68H 时间戳:-1488345920790 Ke7KB2F1UKh8LoWcCY3 时间戳:-1488345945825 KeHlQIBnOE3diP8Wksh 时间戳:-1488521122407 KeI9nJtt4eg5Vd0rcAv 时间戳:-1488527774123 KeWwWW_ezG-cjmF8TNO 时间戳: -1488775680799 KeX7G0WOVidrMBbuGiT 时间戳:-1488778758960 Keg3mtNpyKStpVMMvRm 时间戳:-1488945623757 Keg4fN3rAN7uy5weFJz 时间戳:-1488945855094 下面的命令是我在打印datasnapshot时得到的: KeX7G0WOVidrMBbuGiT 时间戳:-1488778758960 Keg3mtNpyKStpVMMvRm 时间戳:-1488945623757 Keg4fN3rAN7uy5weFJz 时间戳:-1488945855094 Ke7K4vXyEt4gKnjf68H 时间戳:-1488345920790 KeWwWW_ezG-cjmF8TNO 时间戳:-1488775680799 KeHlQIBnOE3diP8Wksh 时间戳:-1488521122407 Ke7KB2F1UKh8LoWcCY3 时间戳:-1488345945825 KeI9nJtt4eg5Vd0rcAv 时间戳:-1488527774123 解决方案当您对Firebase数据库执行查询时,它会为每个匹配的节点返回三条信息: 值该节点 该节点的键 该节点相对于结果中其他节点的顺序 打印快照时,实际上是打印查询结果的 Map< String,Object> 。在地图中,只有两个信息空间:每个节点的密钥和该节点的值。因此,当你打印结果时,节点的顺序会丢失。 因此,你应该使用内置的 getChildren()快照方法: preublic $ onDataChange(DataSnapshot dataSnapshot){$ (DataSnapshot child:dataSnapshot.getChildren()){ Log.d(datasnapshot,child.getKey()+:+ child.getValue()); $ b 如果你想保留在列表中(以便保持顺序),我建议你保留一个快照列表:公共无效onDataChange(DataSnapshot dataSnapshot){ List< DataSnapshot> list = new LinkedList< DataSnapshot>(); (DataSnapshot child:dataSnapshot.getChildren()){ Log.d(datasnapshot,child.getKey()+:+ child.getValue()); list.add(child); } } Query used:q = FirebaseDatabase.getInstance().getReference().child("products").orderByChild("timestamp");q.addValueEventListener(new ValueEventListener() { @Override public void onCancelled(DatabaseError databaseError) { } @Override public void onDataChange(DataSnapshot dataSnapshot) { Log.d("datasnapshot", dataSnapshot.toString());This value is in my Firebase database in same order.Note: I am using negative value approach { -1 * new Date().getTime();} to store timestamp.This value is in my Firebase database in same order:Ke7K4vXyEt4gKnjf68H timestamp: -1488345920790Ke7KB2F1UKh8LoWcCY3timestamp: -1488345945825KeHlQIBnOE3diP8Wkshtimestamp: -1488521122407KeI9nJtt4eg5Vd0rcAvtimestamp: -1488527774123KeWwWW_ezG-cjmF8TNOtimestamp: -1488775680799KeX7G0WOVidrMBbuGiTtimestamp: -1488778758960Keg3mtNpyKStpVMMvRmtimestamp: -1488945623757Keg4fN3rAN7uy5weFJztimestamp: -1488945855094Below order is what I get when I print the datasnapshot:KeX7G0WOVidrMBbuGiTtimestamp: -1488778758960Keg3mtNpyKStpVMMvRmtimestamp: -1488945623757Keg4fN3rAN7uy5weFJztimestamp: -1488945855094Ke7K4vXyEt4gKnjf68Htimestamp: -1488345920790KeWwWW_ezG-cjmF8TNOtimestamp: -1488775680799KeHlQIBnOE3diP8Wkshtimestamp: -1488521122407Ke7KB2F1UKh8LoWcCY3timestamp: -1488345945825KeI9nJtt4eg5Vd0rcAvtimestamp: -1488527774123 解决方案 When you execute a query against the Firebase Database, it returns three pieces of information for each matching node:the value of that nodethe key of that nodethe order of that node relative to the other nodes in the resultWhen you print the snapshot, you're actually printing a Map<String, Object> of the query result. And in a map there is only space for two pieces of information: the key of each node and the value of that node. So the order of the nodes is lost when you print the result.For that reason you should iterate over the children of the result with the built-in getChildren() method of the snapshot:public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot child: dataSnapshot.getChildren()) { Log.d("datasnapshot", child.getKey()+": "+child.getValue()); }}And with this you will see the children in the order of their timestamp.If you want to keep them in a list (so that the order is maintained), I recommend simply keeping a list of snapshots:public void onDataChange(DataSnapshot dataSnapshot) { List<DataSnapshot> list = new LinkedList<DataSnapshot>(); for (DataSnapshot child: dataSnapshot.getChildren()) { Log.d("datasnapshot", child.getKey()+": "+child.getValue()); list.add(child); }} 这篇关于在负面时间戳中按日期排序在Firebase中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 22:36