简体   繁体   English

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

[英]How do I retrieve multiple items of data from Firebase and merge these strings together?

I have a Firebase database which stores data for student timetables. 我有一个Firebase数据库,用于存储学生时间表的数据。 The locations of the multiple items of data are as follows: 多个数据项的位置如下:

Module = Timetable > Course > Day > Time > MODULE_DATA 模块=时间表>课程>天>时间> MODULE_DATA

Room = Timetable > Course > Day > Time > ROOM_DATA 会议室=时间表>课程>天>时间> ROOM_DATA

My aim is to retrieve these items of data into two Strings and merge them together into a single String. 我的目的是将这些数据项检索到两个字符串中,然后将它们合并到一个字符串中。 However the problem I came across that retrieving items of data require separate onDataChange methods which means I cannot access the string outside of this in order to merge it with another. 但是我遇到的问题是,检索数据项需要单独的onDataChange方法,这意味着我无法访问此字符串之外的字符串以将其与另一个字符串合并。

This is my code to retrieve the Module data: 这是我获取模块数据的代码:

                //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) {

            }
        });
    }

Effectively I want to do the same thing for the following Database reference and merge the string values together: 实际上,我想对以下数据库引用执行相同的操作,并将字符串值合并在一起:

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

Assuming you structured your database the way I think you did, this should work. 假设您按照我认为的方式构建了数据库,那么这应该可以工作。 I'm confused as to why you said you couldn't do this in one ValueEventListener. 我对您为什么不能在一个ValueEventListener中做到这一点感到困惑。 I also took out that loop. 我也删除了该循环。

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) {

        }
    });

Edit 编辑

With loop: 带循环:

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) {

            }
        });
}

I found the way to return the data using snapshot foreach 我找到了使用快照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);
      })

      })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM