gpt4 book ai didi

java - 从 Firebase 导入 Java 类

转载 作者:行者123 更新时间:2023-11-29 19:15:40 25 4
gpt4 key购买 nike

我了解如何使用以下代码将类推送到 Firebase:

WeekViewEvent event = new WeekViewEvent();        
DatabaseReference myRef = database.getReference();
myRef.child("Events").push().setValue(event);

我想知道将此类(或任何其他基于 id 或字段的类)检索回 Android 的最简单方法是什么?我敢肯定有一个简单的方法,但到目前为止我发现以下方法不起作用:

    public void importSchedule(String ownerName){
DatabaseReference events = FirebaseDatabase.getInstance().getReference("Events");

Query allOwnersEvents = events.equalTo(ownerName);

allOwnersEvents.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot post : dataSnapshot.getChildren()) {
WeekViewEvent allDay = post.child(WeekViewEvent.class); // This doesn't compile, as the type needs to be String
}
}
public void onCancelled(DatabaseError databaseError) {}
});

WeekViewEvent 对象具有以下变量:

public class WeekViewEvent {
private long mId;
private Calendar mStartTime;
private Calendar mEndTime;
private String mName;
private String mLocation;
private int mColor;
private boolean mAllDay;
private String owner;

//setters and getters
}

在我的 Firebase 中,我要查询的对象是:

Events{
-siX1hB9k-HpCmfO3n:{
allDay:"false",
color:"-477870",
endTime:{
endHour:4,
endMinute:10,
...},
id: 1,
location:"location",
name: "hi",
owner: "a@a.ca",
startTime:{
startHour:3,
startMinute:45,
...}
}
}

最佳答案

要修复此问题,请使用此代码:

DatabaseReference myRef = database.getReference();
DatabaseReference myNewRef = myRef.child("Events").push();
String pushedKey = myNewRef.getKey();
myNewRef.setValue(event);

并取回数据:

public void importSchedule(String ownerName){

DatabaseReference events = FirebaseDatabase.getInstance().getReference("Events").child(pushedKey);
Query allOwnersEvents = events.equalTo(ownerName);

allOwnersEvents.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot post : dataSnapshot.getChildren()) {
String allDay = post.child("yourFiedName").getValue(String.class);
}
}
public void onCancelled(DatabaseError databaseError) {}
});

其中yourFiedName 是所需字段的名称。希望对您有所帮助。

关于java - 从 Firebase 导入 Java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com