Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

android - Handle data returned by an Async task (Firebase)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...