Android OrderByChild()未对我的firebase数据进行排序

Android OrderByChild()未对我的firebase数据进行排序,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我是firebase的新手,我想按时间戳对数据进行排序,下面是我的数据库,这里的键是时间戳 下面是我检索数据的代码 FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("MainDoor"); Query dataOrderedByKey = myRef.orderByChild("{pushId}/key"); dataOrdere

我是firebase的新手,我想按时间戳对数据进行排序,下面是我的数据库,这里的键是时间戳

下面是我检索数据的代码

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("MainDoor");
Query dataOrderedByKey = myRef.orderByChild("{pushId}/key");
dataOrderedByKey.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

Map<String, MyListData> value = ((HashMap<String, MyListData>) dataSnapshot.getValue());
这里我得到的值不是按键的顺序排列的

我得到的数据如下所示,不符合排序顺序

地图中的数据按定义未排序。因此,当您调用dataSnapshot.getValue时,有关dataSnapshot中项目顺序的所有信息都是列表

要维护顺序,请使用支持该顺序的数据结构(如列表),并通过循环DataSnapshot.getChildren以正确的顺序提取各个项

比如:

dataOrderedByKey.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
      System.out.println(snapshot.getKey());
      MyListData value = snapshot.getValue()
    }
  }
  ...
地图中的数据按定义未排序。因此,当您调用dataSnapshot.getValue时,有关dataSnapshot中项目顺序的所有信息都是列表

要维护顺序,请使用支持该顺序的数据结构(如列表),并通过循环DataSnapshot.getChildren以正确的顺序提取各个项

比如:

dataOrderedByKey.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
      System.out.println(snapshot.getKey());
      MyListData value = snapshot.getValue()
    }
  }
  ...

您还可以按排序顺序从firebase获取数据。请参阅我在

上的答案,您也可以按排序顺序从firebase获取数据。请参阅我的答案,您想按日期排序,对吗?试试看

   FirebaseDatabase database = FirebaseDatabase.getInstance();
   DatabaseReference myRef = database.getReference("MainDoor");
   myRef.orderByChild('date').addChildEventListener(new ChildEventListener() {
    // implement the ChildEventListener methods as documented above

   });

你想按日期排序,对吗?试试看

   FirebaseDatabase database = FirebaseDatabase.getInstance();
   DatabaseReference myRef = database.getReference("MainDoor");
   myRef.orderByChild('date').addChildEventListener(new ChildEventListener() {
    // implement the ChildEventListener methods as documented above

   });

由于JavaScript对象的工作方式,val返回的JavaScript对象中的数据顺序不能保证与服务器上的顺序匹配,也不能保证与child_添加事件的顺序匹配。这就是forEach派上用场的地方。它保证DataSnapshot的子项将按照查询顺序进行迭代

    // Assume we have the following data in the Database:
{
  "users": {`enter code here`
    "ada": {
      "first": "Ada",
      "last": "Lovelace"
    },
    "alan": {
      "first": "Alan",
      "last": "Turing"
    }
  }
}

// Loop through users in order with the forEach() method. The callback
// provided to forEach() will be called synchronously with a DataSnapshot
// for each child:
var query = firebase.database().ref("users").orderByKey();
query.once("value")
  .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      // key will be "ada" the first time and "alan" the second time
      var key = childSnapshot.key;
      // childData will be the actual contents of the child
      var childData = childSnapshot.val();
  });
});

由于JavaScript对象的工作方式,val返回的JavaScript对象中的数据顺序不能保证与服务器上的顺序匹配,也不能保证与child_添加事件的顺序匹配。这就是forEach派上用场的地方。它保证DataSnapshot的子项将按照查询顺序进行迭代

    // Assume we have the following data in the Database:
{
  "users": {`enter code here`
    "ada": {
      "first": "Ada",
      "last": "Lovelace"
    },
    "alan": {
      "first": "Alan",
      "last": "Turing"
    }
  }
}

// Loop through users in order with the forEach() method. The callback
// provided to forEach() will be called synchronously with a DataSnapshot
// for each child:
var query = firebase.database().ref("users").orderByKey();
query.once("value")
  .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      // key will be "ada" the first time and "alan" the second time
      var key = childSnapshot.key;
      // childData will be the actual contents of the child
      var childData = childSnapshot.val();
  });
});

非常感谢@Frank van Puffelen,它正在工作。。谢谢你的指导,我试着先投票给你的答案,但这表明我需要超过15个声誉,但无论如何,非常感谢你的支持,你为我节省了很多钱,因为时间就是金钱,你为我节省了时间……非常感谢你@Frank van Puffelen,它正在发挥作用。。谢谢你的指导,我试着首先投票给你的答案,但这表明我需要超过15个声誉,但无论如何,非常感谢你的支持,你为我节省了很多钱,因为时间就是金钱,你为我节省了时间。。。。