简体   繁体   English

如何从匿名内部类更改侧方法中变量的值?

[英]How can I change value of variable in side method from anonymous inner class?

How can I change the value of a variable inside the method from anonymous inner class, to access this variable it must be a final , but if the variable final can't change its value, how solve this problem using java?如何从匿名内部类更改方法内部变量的值,要访问此变量,它必须是final ,但是如果变量 final 无法更改其值,如何使用 java 解决此问题?

This is My Code, I want to change the value of addFlag .这是我的代码,我想更改addFlag的值。

public static boolean addUser(UserModle user){
    Boolean addFlag ;
    dbUser = FirebaseDatabase.getInstance().getReference("user");

    Task task = dbUser.child(user.getId()).setValue(user);
    task.addOnSuccessListener(new OnSuccessListener() {
        @Override
        public void onSuccess(Object o) {
            addFlag  = true;
        }
    });

    task.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {

        }
    });
    return addFlag;
}

IMHO, the current implementation of the addUser function is flawed.恕我直言, addUser函数的当前实现存在缺陷。 As per my understanding, you want to know if the user was added successfully to your firebase database based on the addFlag value that is returned from this function.根据我的理解,您想知道用户是否已根据此函数返回的addFlag值成功添加到您的addFlag数据库中。

The addFlag will be updated once the Firebase database call will get back the data from the firebase realtime database.一旦 Firebase 数据库调用从 Firebase 实时数据库中取回数据, addFlag将被更新。 However, you are returning the flag immediately and hence you are not waiting for the result from your background network thread.但是,您将立即返回标志,因此您无需等待后台网络线程的结果。

In order to achieve that, I would like to suggest an interface based implementation.为了实现这一点,我想建议一个基于interface的实现。 You might have an interface as follows.您可能有如下interface

public interface FirebaseListener {
    void onSuccess(boolean flag);
}

Then add an extra parameter in your addUser function to pass that interface from your Activity or Fragment where you are calling this function.然后在addUser函数中添加一个额外的参数,以从调用此函数的ActivityFragment传递该interface Hence the function might look as follows.因此,该函数可能如下所示。

// Change the return type to void, as we are not expecting anything from this function. 
// Instead, we will wait for the success callback using the interface
public static void addUser(UserModle user, FirebaseListener listener) {

    dbUser = FirebaseDatabase.getInstance().getReference("user");

    Task task = dbUser.child(user.getId()).setValue(user);
    task.addOnSuccessListener(new OnSuccessListener() {
        @Override
        public void onSuccess(Object o) {
            listener.onSuccess(true);
        }
    });

    task.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            listener.onSuccess(false);
        }
    });
}

Now implement the listener in your activity or fragment as follows.现在在您的活动或片段中实现侦听器,如下所示。

public MyActivity extends AppCompatActivity implements FirebaseListener {
    // ... Other functions of your activity 
    @Override
    public void onSuccess(boolean flag) {
        if (flag) {
            // User add successful. Do something here
        } else {
            // User add not successful. Do something here
        }
    }
}

Now while calling the addUser function, you should pass the callback listener along with it as follows.现在,在调用addUser函数时,您应该将回调侦听器与它一起传递,如下所示。

addUser(user, this);

I hope that helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM