简体   繁体   中英

Import Java class from Firebase

I understand how to push a class to Firebase using the following code:

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? 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:

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:

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. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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