处理异步任务(Firebase)返回的数据

时间:2017-02-08 19:17:01

标签: android asynchronous firebase firebase-realtime-database

我正在尝试弄清楚如何对从AsyncTask返回的数据执行某些操作。

我有一个活动,我在调用异步Firebase get操作(操作存在于另一个类中),我想更新Activity中存在的TextView,其大小与检索到的ArrayList相同。这是我的Firebase调用,它在Activity onCreate中被调用:

public void getAttendants() {

   ArrayList<AttendModel> attendees = new ArrayList<AttendModel>();
    FirebaseConnection.getInstance().getAllAttendeesFor(uuid, attendees);

    Log.d("attendees", String.valueOf(attendees.size()));
}

这是我的Firebase操作:

 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) {

        }
    });

}

Android中最好的方法是什么?是否有可能实现排序适配器或完成处理程序?

1 个答案:

答案 0 :(得分:3)

您正在寻找的是Callback接口,根据您的需要,您需要使用单个参数实现单个方法的简单接口,因为没有开箱即用的Java 6(Android Java版本)约定您可以轻松创建它你自己:

interface Callback {

void act(List<AttendModel> models);
}

将它作为参数传递给firebase包装方法,并使用结果调用它。

public void getAllAttendeesFor(String UUID, final ArrayList<AttendModel> attendArray, final Callback callback) {
    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.AttendObjectUUID);
                String userUsername = (String) attend.get(Constants.AttendObjectUserUsername);

                AttendModel attendModel = new AttendModel(userUsername, UUID);
                attendArray.add(attendModel);

                callback.act(attendArray)
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

然后在你的上层方法:

public void getAttendants() {

   ArrayList<AttendModel> attendees = new ArrayList<AttendModel>();
    FirebaseConnection.getInstance().getAllAttendeesFor(uuid, attendees, 
        new Callable<List<AttendModel) {
          void act(List<AttendModel> attendees){
            //here you can update the UI
            Log.d("attendees", String.valueOf(attendees.size()));
          }
        };


}

请注意,只有在主线程上调用firebase查询的onDataChange方法时才能更新UI。