0

For a school project i am trying to make an application to provide users with information about certain events in their region.

The data of the events (location, time, start, etc.) is stored in Firebase Realtime Database. I would like to retrieve this data and store them in an ArrayList of events (on app start or a map activity). I want to use this data to add markers on a Google Map.

This is my Event class.

public class Event {
private String name;
private String organisator;
private String date;
private String startHour;
private String location;
private String description;
private LatLng latLng;


public Event(String name, String organisator, String date, String startHour, String location, String description, LatLng latLng) {
    this.name = name;
    this.organisator = organisator;
    this.date = date;
    this.startHour = startHour;
    this.location = location;
    this.description = description;
    this.latLng = latLng;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getOrganisator() {
    return organisator;
}

public void setOrganisator(String organisator) {
    this.organisator = organisator;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getStartHour() {
    return startHour;
}

public void setStartHour(String startHour) {
    this.startHour = startHour;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public LatLng getLatLng() { return latLng; }

public void setLatLng(LatLng latLng) { this.latLng = latLng; }
}

Firebase data example

How do i put this data in an ArrayList on startup or when the map activity is loaded?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Vito Mastroianni
  • 87
  • 1
  • 1
  • 7

1 Answers1

0

To load data from Firebase database into ArrayList during startup you have to implement code that is responsible for downloading data in the "launcher" activity of your Android app, for example in activity with intent filters declared in manifest file as such:

<intent-filter>    
    <action android:name="android.intent.action.MAIN" />    
    <category android:name="android.intent.categor.LAUNCHER" />    
</intent-filter> 

If you want to load series of data from Firebase database you have to loop through the items returned from request that target your database events directory using FirebaseDatabase.getInstance().getReference().child("events").addValueEventListener(...) and then method to process received objects:

private List<Event> processDataSnapshot(DataSnapshot dataSnap) {
    List<Event> temp = new ArrayList<>();
    for (DataSnapsot data : dataSnap.getChildren()) {
        Event event = data.getValue(Event.class);
        temp.add(event);
    }
    return temp;
}
h3wro
  • 62
  • 1
  • 13