为了账号安全,请及时绑定邮箱和手机立即绑定

Android - 将具有 Firebase 功能的侦听器排除在类之外

Android - 将具有 Firebase 功能的侦听器排除在类之外

潇湘沐 2021-05-31 12:16:14
我想在 MVC 中编写我的应用程序。问题是我是 Android 新手,如果函数不在主类中,我不知道如何使用listener/ callback。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.你知道我怎么做吗?我习惯于快速编码,它会是这样的:func addUser(id: String, completion: @escaping (User) -> Void) {      // Code and then      completion(user)   }
查看完整描述

3 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

您应该为此创建自己的自定义侦听器,MyFirebaseListener并通过实现此接口来更新您的活动内容


public interface MyFirebaseListener {

    void onSuccess(DocumentReference documentReference)

    void onFailure(Exception e)

}

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


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)

                    }

                });


    }

}

MyFirebaseListener在您的 Activity 中实现接口,因此您需要覆盖以下方法并在这些实现的方法中执行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 逻辑和服务逻辑的方法


查看完整回答
反对 回复 2021-06-02
?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

您可以使用两种不同的方法......第一:你可以制作一个回调接口,就像我们在 RecyclerView 点击回调的情况下所做的一样......


第二:如果你必须对 rxJava2、arch life 或 Agera 有一点了解,你可以使用 livedata 观察者......


所以让我们考虑第一个方法


考虑你的班级是


class otherClass{

callbackInterface mCallBackInterface;


public(callbackInterface mCallBackInterface){

this.mCallBackInterface=mCallBackInterface;

}


interface callbackInterface{

void onSucuss(DocumentReference documentReference);

}



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


mCallBackInterface.onSucuss(decumentReference);



                    }

                })

                .addOnFailureListener(new OnFailureListener() {

                    @Override

                    public void onFailure(@NonNull Exception e) {

                        Log.d(TAG, "Error adding document", e);

                    }

                });


    }


}


///The Class which will be calling it will be something like this


class CallingClass implements CallBackInterface{


@Override

void CallBackINterface(DocumentReference documentReference){


//Your code goes here

}

}


这将完成工作广播接收器也可以使用,但上述解决方案最适合初学者......


查看完整回答
反对 回复 2021-06-02
  • 3 回答
  • 0 关注
  • 177 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号