I have a Firebase Database. I want to create a query and put it in a method in a DBUtils class so that I can call the query from different activities in an Android project.
For Example, something like :
ArrayList<String> aStringArrayList = DBUtils.GetAllJobsOnADate(Date date);
.
.
.
// use aStringArrayList to populate a listview.
AND
public static ArrayList<String> GetJobsOnADate(Date date) {
final ArrayList<String> mReturnList = new ArrayList<String>();
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("jobs")
.whereEqualTo("job_date", date)
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots) {
mReturnList.add(queryDocumentSnapshot.get("job_title"));
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// failure code
}
});
return mReturnList;
}
When I debug the app, I can see that the get() does indeed return a list of QuerySnapshotDocument (can see the breakpoint being hit in onSuccessListner code) but aStringArray always is empty.
Tried using AsyncTask (onPostExecute). Doesn't work either.
Of course, works fine if the code is in a function in the same activity. But then that is a lot of redundant code spread over many activities.
Any idea?