第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Android - 將具有 Firebase 功能的偵聽(tīng)器排除在類(lèi)之外

Android - 將具有 Firebase 功能的偵聽(tīng)器排除在類(lèi)之外

瀟湘沐 2021-05-31 12:16:14
我想在 MVC 中編寫(xiě)我的應(yīng)用程序。問(wèn)題是我是 Android 新手,如果函數(shù)不在主類(lèi)中,我不知道如何使用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 類(lèi)中使用這個(gè)函數(shù)。但是,如果我這樣做,我不知道如何僅在函數(shù)完成執(zhí)行時(shí)才能啟動(dòng)操作 -> 換句話說(shuō),當(dāng)它是addOnSuccessListener.你知道我怎么做嗎?我習(xí)慣于快速編碼,它會(huì)是這樣的:func addUser(id: String, completion: @escaping (User) -> Void) {      // Code and then      completion(user)   }
查看完整描述

3 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

您應(yīng)該為此創(chuàng)建自己的自定義偵聽(tīng)器,MyFirebaseListener并通過(guò)實(shí)現(xiàn)此接口來(lái)更新您的活動(dòng)內(nèi)容


public interface MyFirebaseListener {

    void onSuccess(DocumentReference documentReference)

    void onFailure(Exception e)

}

現(xiàn)在將Activity 作為參數(shù)傳遞給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 中實(shí)現(xiàn)接口,因此您需要覆蓋以下方法并在這些實(shí)現(xiàn)的方法中執(zhí)行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 邏輯和服務(wù)邏輯的方法


查看完整回答
反對(duì) 回復(fù) 2021-06-02
?
慕無(wú)忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以使用兩種不同的方法......第一:你可以制作一個(gè)回調(diào)接口,就像我們?cè)?RecyclerView 點(diǎn)擊回調(diào)的情況下所做的一樣......


第二:如果你必須對(duì) rxJava2、arch life 或 Agera 有一點(diǎn)了解,你可以使用 livedata 觀察者......


所以讓我們考慮第一個(gè)方法


考慮你的班級(jí)是


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

}

}


這將完成工作廣播接收器也可以使用,但上述解決方案最適合初學(xué)者......


查看完整回答
反對(duì) 回復(fù) 2021-06-02
  • 3 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)