[英]Read data from Firebase database when I have multiple unique ids
I'm having trouble writing in my database because I have multiple unique ids that I don't know how to access.我在写入数据库时遇到问题,因为我有多个不知道如何访问的唯一 ID。 I know that for writing I need to use something like this:
我知道为了写作我需要使用这样的东西:
DatabaseReference ref=database.getReference().child("Children")
ChildData data = new ChildData(fullName,age);
ref.push().setValue(data);
My database looks like this:我的数据库看起来像这样:
{
"Children" : {
"55hxObZjRLY9PSZxZxGgSTRKzpc2" : {
"-MzUih5e40OsWPF_Dj0q" : {
"age" : "22",
"fullName" : "Cristina "
},
"plays" : {
"-MznnNih5fItYf2usXB4" : {
"centiseconds" : "70",
"currentDate" : "01.04.2022",
"currentHour" : "04:23:30",
"numberOfFails" : "5",
"seconds" : "2"
}
}
}
},
"Data" : {
"id1" : {
"centiseconds" : "70",
"currentDate" : "01.04.2022",
"currentHour" : "04:23:30",
"numberOfFails" : "5",
"seconds" : "2"
}
}
}
I need the "plays" child to be under "fullName" in "-MzUih5e40OsWPF_Dj0q", not as a separated child.我需要“播放”孩子在“-MzUih5e40OsWPF_Dj0q”中的“全名”下,而不是作为一个分离的孩子。 How can I access the path without hardcoding it like this
ref.child("MzUih5e40OsWPF_Dj0q").child("plays").push().setValue(data)
?如何在不像这样硬编码的情况下访问路径
ref.child("MzUih5e40OsWPF_Dj0q").child("plays").push().setValue(data)
? I will have multiple children in "Children" with different ids, I won't be able to harcode them.我将在“孩子”中有多个具有不同 ID 的孩子,我将无法对它们进行硬编码。
Is there any function that returns the path for the unique id?是否有任何 function 返回唯一 ID 的路径?
Here is the entire function:这是整个 function:
public void writeNewData
(String fullName,String age) {
DatabaseReference reference = database.getReference().child("Data");
DatabaseReference ref=myRef.push();
ChildData data = new ChildData(fullName,age);
ref.push().setValue(data);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
DatabaseReference playRef=ref.child("plays");
DatabaseData databaseData = ds.getValue(DatabaseData.class);
playRef.push().setValue(databaseData);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
Edit: This is my desired schema:编辑:这是我想要的架构:
{
"Children" : {
"55hxObZjRLY9PSZxZxGgSTRKzpc2" : {
"-MzUih5e40OsWPF_Dj0q" : {
"age" : "22",
"fullName" : "Cristina ",
"plays" : {
"-MznnNih5fItYf2usXB4" : {
"centiseconds" : "70",
"currentDate" : "01.04.2022",
"currentHour" : "04:23:30",
"numberOfFails" : "5",
"seconds" : "2"
}
}
},
}
},
"Data" : {
"id1" : {
"centiseconds" : "70",
"currentDate" : "01.04.2022",
"currentHour" : "04:23:30",
"numberOfFails" : "5",
"seconds" : "2"
}
}
}
If you want the plays
information to be a sibling of the age
and fullName
properties, then you will need to have a plays
field/property in your ChildData
class.如果您希望
plays
信息成为age
和fullName
属性的兄弟,那么您需要在ChildData
class 中有一个plays
字段/属性。
You'll need two classes, one to represent a "play" and one to represent a "child".您将需要两个类,一个代表“游戏”,一个代表“孩子”。 At its minimum these will look like:
这些至少看起来像:
public class Play {
public String centiseconds, currentDate, currentHour, numberOfFails, seconds;
}
public class Child {
public String age, fullName;
public Map<String, Play> plays = new HashMap<>();
}
If you read/write this class, you will get plays
nested under the child's data.如果你读/写这个 class,你会得到嵌套在孩子数据下的
plays
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.