[英]How to access data from firebase Realtime database child?
My realtime database json我的实时数据库 json
"module1" : {
"Operating System" : {
"pdfName" : "pdf",
"pdfUrl" : "gs://knec-pastpapers.appspot.com/module1/OS/july2007.pdf"
}
} }
I have a problem accessing the fields under Operating System and display the data in Recycler view.我在访问操作系统下的字段并在回收站视图中显示数据时遇到问题。 This is my model class这是我的 model class
public class viewModel {
public String pdfName;
public String pdfUrl;
public viewModel() {
}
public viewModel(String pdfName, String pdfUrl) {
this.pdfName = pdfName;
this.pdfUrl=pdfUrl;
}
public String getPdfName() {
return pdfName;
}
public void setPdfName(String pdfName) {
this.pdfName = pdfName;
}
public String getPdfUrl() {
return pdfUrl;
}
public void setPdfUrl(String pdfUrl) {
this.pdfUrl = pdfUrl;
}
This is how i am trying to access the fields from realtime database这就是我尝试从实时数据库访问字段的方式
databaseReference= FirebaseDatabase.getInstance().getReference("ict").child("Operating System");
getImageData();
}
private void getImageData() {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot di:dataSnapshot.getChildren()){
viewModel articleList=di.getValue(viewModel.class);
articleLists.add(articleList);
}
loadpdfsAdapter adapter=new loadpdfsAdapter(articleLists,getApplicationContext());
rv.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
After running the code my recyclerview is empty.运行代码后,我的 recyclerview 是空的。 My question is how can i access the child nodes under Operating System?我的问题是如何访问操作系统下的子节点?
The for (DataSnapshot di:dataSnapshot.getChildren()){
is only needed when the snapshot you get has multiple viewModel
child nodes.仅当您获得的快照具有多个viewModel
子节点时,才需要for (DataSnapshot di:dataSnapshot.getChildren()){
。 But since you're loading data from /ict/Operating System
there is only a single viewModel
child node.但是由于您从/ict/Operating System
加载数据,因此只有一个viewModel
子节点。
You either have to remove the loop:您要么必须删除循环:
databaseReference= FirebaseDatabase.getInstance().getReference("ict").child("Operating System");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
viewModel articleList=dataSnapshot.getValue(viewModel.class);
articleLists.add(articleList);
...
Or you can load all of ict
and then loop over the child nodes:或者您可以加载所有ict
然后循环遍历子节点:
databaseReference= FirebaseDatabase.getInstance().getReference("ict");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot di:dataSnapshot.getChildren()){
viewModel articleList=di.getValue(viewModel.class);
articleLists.add(articleList);
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.