android中的Firebase orderByChild;无法让它发挥作用

时间:2016-09-12 15:59:04

标签: android firebase firebase-realtime-database

我的数据结构为:

https://law-apps-44h221543638e.firebaseio.com/apps to promote/0/ >> name:"First App", package:"fra"
https://law-apps-44h221543638e.firebaseio.com/apps to promote/1/ >> name:"Second App", package:"sca"
https://law-apps-44h221543638e.firebaseio.com/apps to promote/2/ >> name:"Third App", package:"tha"

我用

查询它
Firebase myFirebaseReference = new Firebase("https://law-apps-44h221543638e.firebaseio.com/apps to promote");
Query queryRef = myFirebaseReference.orderByChild("name");

queryRef.addListenerForSingleValueEvent(new ValueEventListener() { // override methods })

但它以相同的顺序返回数据,即按第一个孩子(1,2,3等)排序 我该如何查询它以便按每个孩子的“name”标签对数据进行排序?

2 个答案:

答案 0 :(得分:0)

好的,所以我找到了这个问题的答案,我正在写它,以便其他人可以从中受益。我正在使用一种较旧的技术,其中我使用他们父母的数字从datasnapshot我的应用程序中找到了这个,因此,我明确地撤消了订购。

现在我使用了dataSnapshot.getChildren()。iterator(),现在它正常工作。这是代码:

Query queryRef = myFirebaseReference.orderByChild("name");

    queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

    String name;
    String my_package;

    long lengthOfForLoop = dataSnapshot.getChildrenCount();
    Iterator<DataSnapshot> child = dataSnapshot.getChildren().iterator();
    for (int i = 0; i < lengthOfForLoop; i++) {
            DataSnapshot next = child.next();
            name = next.child("name").getValue(String.class);
            my_package = next.child("package").getValue(String.class);
              // do something with this data.
               }
        }
    });
}

答案 1 :(得分:0)

谢谢,乌斯曼!

我在for循环中对子级可迭代集合使用了相同的代码。 这段代码为我工作(在Kotlin中):

 accountsReference.child(accountId).child("actions").orderByChild("actionPosition").addListenerForSingleValueEvent( object : ValueEventListener {
            override fun onDataChange(var1: DataSnapshot) {
                if (var1.children != null) {
                    for (actionsEntries in var1.children) {
                       ...
                    }
                }