2

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

}

Jason Berryman
  • 4,760
  • 1
  • 26
  • 42

1 Answers1

1

You cannot declare a variable of type Event as global and simply use it outside the onSuccess() method. This is happening because this method has an asynchronous behaviour, which means that is called even before you are getting the data from the database. A quick fix would be to use the Event object only inside the onSuccess() method or if you want to use it outside, please use the last part of my answer from this post. You can also take a look at this video.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks so much for this answer. I figured the first part out but good to see there is another way. I'll take a look at this for future reference! – Stephen Giles Mar 18 '18 at 15:36