【发布时间】:2018-11-27 18:18:09
【问题描述】:
我正在使用 android studio 创建一个考勤应用程序。其中一部分包括一个历史按钮,其中列出了学生的个人出勤历史。但是,我在指定路径时无法读取数据。我用谷歌搜索了各种解决方案,但我似乎无法弄清楚。有什么想法吗?
package com.example.android.teamnahhseproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.*;
public class attendance_history_student extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference("teamnahh-7d81b/student_users/Alisha Tapiawala/classes/CS 3354_003");
String attendance_text;
TextView textBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance_history_student);
textBox = findViewById(R.id.attendance);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
attendance_text = postSnapshot.getValue(String.class);
textBox.append(attendance_text + "\n");
}
}
@Override
public void onCancelled(DatabaseError databaseError) { }
});
}
}
我正在尝试打印出“CS 3354_003”的所有孩子
【问题讨论】:
-
我的回答是否使用了
ValueEventListener帮助? -
我用这段代码测试了 Log : Log.w(postSnapshot.getKey(), postSnapshot.getValue(String.class)); 和它的工作。 TextView 中出现了什么?
-
好吧....在我的公寓 wifi 而不是我的学校 wifi 上进行测试时,它似乎可以正常工作,哈哈。谢谢大家!
标签: java android firebase android-studio firebase-realtime-database