[英]how do i retrieve a nested object from Firebase?
I am trying to retrieve a nested list of workouts from a Realtime Database and I don't know how to do it.我正在尝试从实时数据库中检索嵌套的锻炼列表,但我不知道该怎么做。
I made some research and still couldn't really figure out how am supposed to do it.我做了一些研究,但仍然无法真正弄清楚应该怎么做。
The Realtime Database JSON file looks like this:实时数据库 JSON 文件如下所示:
I am looking to retrieve data by workout, for example, if someone presses the workout one button I should retrieve the full workout one object.我希望通过锻炼来检索数据,例如,如果有人按下锻炼一按钮,我应该检索完整的锻炼一 object。 but I don't know how am supposed to design my query request nor how am supposed to structure my model object that conceives the received data.
但我不知道应该如何设计我的查询请求,也不知道应该如何构造我的 model object 来构思接收到的数据。
I think below line code help you.我认为下面的代码可以帮助你。
databaseReference = FirebaseDatabase.getInstance().getReference().child("Fat Loss").child("Workout one").child("day 1")
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
allTaskList.clear();
if (snapshot.exists()) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
//modal class object
AddTODOListModal model = dataSnapshot.getValue(AddTODOListModal.class);
assert model != null;
model.setId(dataSnapshot.getKey());
allTaskList.add(model);
}
adapter = new TODOListAdapter(TODOListHomeActivity.this, allTaskList);
binding.rvTODO.setAdapter(adapter);
}else {
Utils.showToast("No Data Available");
}
Utils.dismissProgressDialog();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Utils.showToast(error.getDetails());
}
});
Reference link:-https://firebase.google.com/docs/database/android/lists-of-data参考链接:-https://firebase.google.com/docs/database/android/lists-of-data
As I see in your screenshot, under the "Workout one" node, you have two nested levels of data.正如我在您的屏幕截图中看到的,在“锻炼一”节点下,您有两个嵌套的数据级别。 So to get all exercises for each day, you have to loop over the children twice:
因此,要获得每天的所有练习,您必须循环遍历孩子两次:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference workoutOneRef = db.child("Fat Loss").child("Workout one");
workoutOneRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot daySnapshot : task.getResult().getChildren()) {
for (DataSnapshot exerciseSnapshot : daySnapshot.getChildren()) {
String name = exerciseSnapshot.child("name").getValue(String.class);
Log.d("TAG", name);
}
}
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});
Please also don't forget that the Firebase API is asynchronous.请不要忘记 Firebase API 是异步的。 So any code that needs data from the database needs to be inside the
onComplete()
method, or be called from there.因此,任何需要来自数据库的数据的代码都需要在
onComplete()
方法中,或者从那里调用。 To understand better, I recommend you check the following resource:为了更好地理解,我建议您查看以下资源:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.