gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:04:26 24 4
gpt4 key购买 nike

我想弄清楚如何处理从 AsyncTask 返回的数据。

我有一个 Activity ,我正在调用异步 Firebase get 操作(不同类中存在的操作),我想用检索到的 ArrayList 的大小更新 Activity 中存在的 TextView .这是在 Activity onCreate 中调用的我的 Firebase 调用:

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 中最好的方法是什么?是否有可能实现某种适配器或完成处理程序?

最佳答案

您正在寻找的是回调接口(interface),至于您的需求,您需要使用单个参数实现单个方法的简单接口(interface),因为 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。

关于android - 处理异步任务返回的数据 (Firebase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42121771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com