有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

模块=时间表>;课程>;日>;时间>;模块数据

房间=时间表>;课程>;日>;时间>;房间数据

我的目标是将这些数据项检索到两个字符串中,并将它们合并到一个字符串中。然而,我遇到的问题是,检索数据项需要单独的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) 个答案

  1. # 1 楼答案

    我找到了使用snapshot 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);
          })
    
          })
    
  2. # 2 楼答案

    假设您按照我认为的方式构建了数据库,这应该会起作用。我不明白你为什么说你不能在一个时间内做到这一点。我还取出了那个环

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