gpt4 book ai didi

java - Android - 将具有 Firebase 功能的监听器保留在类之外

转载 作者:行者123 更新时间:2023-12-02 11:00:15 25 4
gpt4 key购买 nike

我想在 MVC 中编写我的应用程序。问题是我是 Android 新手,如果函数不在主类中,我不知道如何拥有监听器/回调

public void addNewUser(String firstname, String lastname, String email, Integer gender, String uid, String profileImageUrl){

Map<String, Object> data = new HashMap<>();
data.put("firstname", firstname);
data.put("lastname", lastname);
data.put("email", email);
data.put("gender", gender);
data.put("boxId", "independent");
data.put("notificationsEnabled", true);
data.put("profileImageUrl", profileImageUrl);

mFirebaseFirestore.collection("usersP").add(data)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
mProgressBar.setVisibility(View.GONE);

mIRegisterActivity.inflateFragment("Register Box", mHashMap);
Log.d(TAG, "DocumentSnapshot written with ID: " + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "Error adding document", e);
}
});

}

我想在不同的 Java 类中使用此函数。但如果我这样做,我不知道如何仅在函数完成执行时才能启动操作 -> 换句话说,当它是 addOnSuccessListener 时。

你知道我该怎么做吗?

我习惯用 swift 编码,它会是这样的:

func addUser(id: String, completion: @escaping (User) -> Void) {
// Code and then
completion(user)
}

最佳答案

您应该为 MyFirebaseListener 创建您的自己的自定义监听器,并通过实现此接口(interface)来更新 Activity 中的内容

public interface MyFirebaseListener {
void onSuccess(DocumentReference documentReference)
void onFailure(Exception e)
}

现在将Activity作为参数传递给MyFirebaseListeneraddNewUser()方法,如下所示

public class UserApi{

public void addNewUser(String firstname,
String lastname,
String email,
Integer gender,
String uid,
String profileImageUrl,
MyFirebaseListener myFirebaseListener){

Map<String, Object> data = new HashMap<>();
data.put("firstname", firstname);
data.put("lastname", lastname);
data.put("email", email);
data.put("gender", gender);
data.put("boxId", "independent");
data.put("notificationsEnabled", true);
data.put("profileImageUrl", profileImageUrl);

mFirebaseFirestore.collection("usersP").add(data)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
myFirebaseListener.onSuccess(documentReference)
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
myFirebaseListener.onFailure(e)
}
});

}
}

在您的 Activity 中实现 MyFirebaseListener 接口(interface),因此您需要重写以下方法,并在这些实现的方法中执行UI修改,如下所示

public class MyActivity extends AppCompatActivity implements MyFirebaseListener {

void someMethod(){
addNewUser(firstname,
lastname,
email,
gender,
uid,
profileImageUrl,
this) // <- This will be reference to Activity with Type of MyFirebaseListener
}

void onSuccess(DocumentReference documentReference){
mProgressBar.setVisibility(View.GONE);
mIRegisterActivity.inflateFragment("Register Box", mHashMap);
Log.d(TAG, "DocumentSnapshot written with ID: " + documentReference.getId());
}
void onFailure(Exception e){
Log.d(TAG, "Error adding document", e);
}
}

这就是您如何使用自定义界面分离UI 逻辑和服务逻辑

关于java - Android - 将具有 Firebase 功能的监听器保留在类之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51394279/

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