I am a beginner so apologies for a possible silly question.
I am trying to retrieve data from a Firebase database. This works but I cannot assign the value to a string variable for use later on.
This is the asynchronous call to the database which returns the right result. (The data its querying from is static so I don't really need an asynchronous call but as far as I am aware, I don't have another option).
public static void getAnimalDetails(String strColl, final String strQueryField, final String strQueryValue,
final MyCallBack myCallback){
mfirebaseDb.collection(strColl)
.whereEqualTo(strQueryField, strQueryValue)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String strResult = document.get("animal_class").toString();
Log.d(TAG, "SSSS:" + strResult );
myCallback.onCallback(strResult);
}
}
}
});
}
This is the callback function passed to the method above.
public interface MyCallBack {
void onCallback(String strValFromAsyncTask);
}
Then this is where I call the asynch task and try and access the data from the callback. This method fires on a button click I can see via the log that the right value is populated in strAnimalClass (which is a global variable) But when I try to use strAnimalClass outside of the call back it is null.
getAnimalDetails("animals", "animal_common_name", animal, new MyCallBack() {
@Override
public void onCallback(String strValFromAsyncTask) {
strAnimalClass = strValFromAsyncTask;
Log.d(TAG, "mmmmm:" + strAnimalClass );
}
});
Can anyone help with how to get a value like this out of the async / callback environment for use later on? Thank you