I am trying to figure out how to do something with data returned from an AsyncTask
.
I have an activity where I am calling an async Firebase get operation (operation existing in a different class), and I want to update an TextView
existing in the Activity with the size of the ArrayList retrieved. Here is my Firebase call that get's called in Activity onCreate:
public void getAttendants() {
ArrayList<AttendModel> attendees = new ArrayList<AttendModel>();
FirebaseConnection.getInstance().getAllAttendeesFor(uuid, attendees);
Log.d("attendees", String.valueOf(attendees.size()));
}
and here is my Firebase operation :
public void getAllAttendeesFor(String UUID, final ArrayList<AttendModel> attendArray) {
final DatabaseReference attendObjects = getDatabaseTableWith(Constants.tableAttendObject);
Query queryRef = attendObjects.orderByChild(Constants.taAttendObjectUUID).equalTo(UUID);
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
HashMap<String, Object> attend = (HashMap<String, Object>) postSnapshot.getValue();
String UUID = (String) attend.get(Constants.taAttendObjectUUID);
String userUsername = (String) attend.get(Constants.AttendObjectUserUsername);
AttendModel attendModel = new AttendModel(userUsername, UUID);
attendArray.add(attendModel);
//here would like to notify or somehow return the attendArray
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
What is the best approach of this in Android? Is there the possibility to implement an Adapter of sorts or a completion handler?
What you are looking for is Callback interface, as for your needs you need simple interface that implement single method with single parameter, as there is no out of the box convention for Java 6 (Android Java version) you can easily create it by yourself:
pass it as an argument to the firebase wrapping method, and call it with the result.
then in your upper level method:
Just be aware that you can update the UI only if onDataChange method of the firebase query is called on the main thread.