如何从Firebase检索多个数据项并将这些字符串合并在一起?

时间:2017-03-04 14:23:36

标签: java android firebase firebase-realtime-database

我有一个Firebase数据库,用于存储学生时间表的数据。多项数据的位置如下:

模块=时间表>课程>日>时间> MODULE_DATA

房间=时间表>课程>日>时间> ROOM_DATA

我的目标是将这些数据项检索到两个字符串中,并将它们合并为一个字符串。然而,我遇到的问题是检索数据项需要单独的onDataChange方法,这意味着我无法访问此外的字符串以便将其与另一个进行合并。

这是我检索模块数据的代码:

                //Monday
    for (i = 9; i <= 18; i++) {
        String time = Integer.toString(i);
        final String MonTime = "Mon" + i;

        DatabaseReference Module = mRef.child("Monday").child(time).child("Module");
        Monday.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String MonValue = dataSnapshot.getValue(String.class);
                int id = getResources().getIdentifier(MonTime, "id", getPackageName());
                TextView Monday = (TextView) findViewById(id);
                Monday.setText(MonValue);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

实际上,我想为以下数据库引用做同样的事情,并将字符串值合并在一起:

            DatabaseReference Room= mRef.child("Monday").child(time).child("Room");

2 个答案:

答案 0 :(得分:0)

假设您按照我认为的方式构建了数据库,这应该可行。我很困惑为什么你说你不能在一个ValueEventListener中做到这一点。我也拿出那个循环。

mRef.child("Monday").orderByKey().startAt("9").endAt("18").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot child : dataSnapshot.getChildren()) {
                HashMap<String, String> value = (HashMap<String, String>) child.getValue();
                String module = value.get("module");
                String room = value.get("room");
                System.out.println(room + module);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

修改

使用循环:

for (i = 9; i <= 18; i++) {
        String time = Integer.toString(i);
        final String MonTime = "Mon" + i;

        dbRef.child("Monday").child(time).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String module = dataSnapshot.child("Module").getValue(String.class);
                String room = dataSnapshot.child("Room").getValue(String.class);
                int id = getResources().getIdentifier(MonTime, "id", getPackageName());
                TextView Monday = (TextView) findViewById(id);
                Monday.setText(MonValue);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
}

答案 1 :(得分:-1)

我找到了使用快照foreach返回数据的方法

 firebase.database().ref('events').once('value',(data)=>{
      //console.log(data.toJSON());
      data.forEach(function(snapshot){
        var newPost = snapshot.val();
        console.log("description: " + newPost.description);
        console.log("interest: " + newPost.interest);
        console.log("players: " + newPost.players);
        console.log("uid: " + newPost.uid);
        console.log("when: " + newPost.when);
        console.log("where: " + newPost.where);
      })

      })