I am trying to retrieve an Events object from Firestore that I have stored and display it in an activity. I am locating the document in a document reference and then using a setEvent method to set the document (converted to an Event object) to "thisEvent" variable declared at the start.
However, when I go to set the TextViews with the information from the event, it doesn't work as thisEvent is null. I've set up an if/else to see if it is null and so everytime I run the app, the event is not displayed and instead HEY THERE is the event title as per the if statement. I have followed the Firestore documentation outlined on their website but it seems I am not going about this the correct way.
public class EventViewActivity extends AppCompatActivity {
//declaring FireBase variables
private FirebaseFirestore mFStore;
private FirebaseAuth mAuth;
//declaring user,team and event variables
private String user_id;
private String team_id;
private String TAG = "EventViewActivity";
String event_id;
Event thisEvent;
//declaring and binding UI elements
@BindView(R.id.event_title_text) TextView event_title;
@BindView(R.id.event_date_text)TextView event_date;
@BindView(R.id.event_time_text)TextView event_time;
@BindView(R.id.event_description_text)TextView event_description;
@BindView(R.id.event_location_text)TextView event_location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_view);
ButterKnife.bind(this);
mAuth = FirebaseAuth.getInstance();
mFStore = FirebaseFirestore.getInstance();
Intent intent = getIntent();
event_id = intent.getExtras().getString("event_id");
Log.d(TAG, "eventid: " + event_id);
//retrieve event information
DocumentReference eventInfoRef = mFStore.collection("Teams/YfLa27NWaaQSfNwhZPgX/Events").document("MhEg0kRdJEedf7YnwvBi");
eventInfoRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
setEvent(documentSnapshot.toObject(Event.class));
}
});
if (thisEvent !=null){
event_title.setText(this.thisEvent.getTitle());
event_date.setText(thisEvent.getDate().toString());
event_time.setText(thisEvent.getTime());
event_description.setText(thisEvent.getDescription());
event_location.setText(thisEvent.getLocation());}
else {
event_title.setText("HEY THERE");
}
}
public void setEvent(Event event){
this.thisEvent = event;
}
}