从Firebase导入Java类

时间:2017-04-25 04:19:47

标签: java android firebase firebase-realtime-database

我了解如何使用以下代码将课程推送到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,
      ...}
    }
}

2 个答案:

答案 0 :(得分:1)

您可以尝试以下方法来检索您的课程'对象

DECLARE @string varchar(20) 
  SELECT @string = 'test complete name'
  SELECT complete_name from users 
    where 1 = dbo.test_complete_name(@string,complete_name)

答案 1 :(得分:1)

要解决问题,请使用以下代码:

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是所需字段的名称。希望它有所帮助。