简体   繁体   English

从Firebase导入Java类

[英]Import Java class from Firebase

I understand how to push a class to Firebase using the following code: 我了解如何使用以下代码将类推送到Firebase:

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

I'm wondering what is the easiest way to retrieve this class (or any other class based on an id, or field) back into Android? 我想知道将此类(或基于id或字段的任何其他类)检索回Android的最简单方法是什么? I'm sure there's an easy way, but all I've found so far is the following which doesn't work: 我敢肯定有一种简单的方法,但是到目前为止,我发现的所有方法都不起作用:

    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) {}
    });

The WeekViewEvent objects have the following variables: 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
}

and in my Firebase, the object I'm trying to query is: 在我的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,
      ...}
    }
}

You can try following method to retrieve your class' object 您可以尝试以下方法来检索您的类的对象

Map<String, Object> eventMap;
for(DataSnapshot da :dataSnapshot.getChildren())
{
    if (da.getValue() instanceof Map) {
        eventMap = (HashMap<String, Object>)
                da.getValue();
        Map<String, Object> eventMap = weekMap;
        WeekViewEvent event = new WeekViewEvent();
    }
}

To fix t his, please use this code: 要解决此问题,请使用以下代码:

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

and to get the data back: 并取回数据:

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) {}
});

In which yourFiedName is the name of desired field. 其中yourFiedName是所需字段的名称。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM