java - 使用其他数据在 Firebase 中获取数据
问题描述
这是我项目的 Firebase 数据库结构。
我想为特定的hostelname 和 roomno获取complaint division
, 。如何获取这些数据并在android项目中显示?describe
stat
解决方案
This is a basic function in Firebase on Android. You can use ValueEventListeners with the database reference to do this. The steps to achieve the desired results can be enlisted as below:
- Create a Complaint model with the same fields as your database.
- Get the right Firebase reference for your database node and add a ValueEventListener instance to listen for database changes.
- Pass a DataSnapshot into the Complaint class and assign it to a Complaint object.
- Do what you want with the Complaint object you have obtained.
Creating a Complaint class:
class Complaint {
// your fields should have the same name as database fields to prevent unnecessary complications
public String complaintdivision;
public String complaintid;
public String describe;
public String hostelname;
public String roomno;
public String stat;
public Complaint(){// required for Firebase
}
}
Getting the data from Firebase:
ArrayList<Complaint> myComplaintArrayList = new ArrayList<>();
FirebaseDatabase.getInstance().getReference().child("Complaints").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot complaint: dataSnapshot.getChildren()){
Complaint c = complaint.getValue(Complaint.class);
myComplaintArrayList.add(c);// you should have an ArrayList<Complaint> for this loop
}
// do what you want with the items you obtained
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
This is pretty much all of it. If you are still having problems, you should read a tutorial on Firebase.
推荐阅读
- flutter - flutter - 如何在 rflutter_alert 对话框中显示 CircularProgressIndicator
- javascript - 从javascript中的查询参数获取对象数组
- python - 如何将 tkinter 窗口设置为仅在用户第一次运行程序时打开并且不再打开?
- elixir - 在 Phoenix / Elixir 中创建性别下拉列表会导致“分配 @all_genders 在 eex 模板中不可用”。
- java - 生成对 REST API 的授权访问的最佳方式是什么?
- python-3.x - 在循环中使用 BeautifulSoup 的输出作为输入
- ssl - 没有 SSL 证书可以视频/音频或其他多媒体流工作吗
- flutter - 如何将多个动画放入一个小部件中?
- networking - 无服务器通信
- r - 我想根据 R 中另一个数据框中的一列一列来划分交叉表中的值