Android Java中如何获取onSuccess方法之外的值?

发布于 2025-01-09 11:24:53 字数 548 浏览 0 评论 0原文

我需要在 Android Java 的 onSuccess 方法之外获取 userRole。如何得到它? 我尝试实现接口和回调函数,但这也不起作用。

documentReference = firebaseFirestore.collection(
                "Users").document(firebaseAuth.getCurrentUser().getUid());
        documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
        userRole = documentSnapshot.getString("userRole");

    }
});

I need to get the userRole outside the onSuccess method in Android Java. How to get that?
I tried implementing interface and callback function and that also not worked.

documentReference = firebaseFirestore.collection(
                "Users").document(firebaseAuth.getCurrentUser().getUid());
        documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
        userRole = documentSnapshot.getString("userRole");

    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2025-01-16 11:24:53

您可以使用两个选项。第一个是使用 Mutablelivedata,第二个是使用接口。假设您使用 MVVM 模式。

public Mutablelivedata<String> getuserRole(){
  documentReference = firebaseFirestore.collection("Users").document(firebaseAuth.getCurrentUser().getUid());
  documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
  @Override
  public void onSuccess(DocumentSnapshot documentSnapshot) {
      userRole.setValue(documentSnapshot.getString("userRole"));
    }
 });
return userRole;
}

然后在您的活动/片段中

yourViewModel.getuserRole.observe(getViewLifecycleOwner(), new new Observer<String>(){
@Overrride
public void onChanged(String s){
//Log.d(TAG, "onChanged: your user role: "+s);
  }
});

第二个是进行接口回调

public interface UserRoleInterface(){
  void userRole(String role);
}

然后将接口设置为您的方法/函数的参数

public void userRole(UserRoleInterface userRoleInterface){
 documentReference = firebaseFirestore.collection("Users").document(firebaseAuth.getCurrentUser().getUid());
 documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
  @Override
  public void onSuccess(DocumentSnapshot documentSnapshot) {
  userRoleInterface.userRole(documentSnapshot.getString("userRole"));
 }
});

然后在您的活动/片段中

yourViewModel.userRole(new UserRoleInterface(){
@Override
public void userRole(String role){
//Log.d(TAG, "onChanged: your user role: "+role);
}
});

There are two options you can use. The first one is using Mutablelivedata and the second is using an Interface. Assuming you are using MVVM pattern.

public Mutablelivedata<String> getuserRole(){
  documentReference = firebaseFirestore.collection("Users").document(firebaseAuth.getCurrentUser().getUid());
  documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
  @Override
  public void onSuccess(DocumentSnapshot documentSnapshot) {
      userRole.setValue(documentSnapshot.getString("userRole"));
    }
 });
return userRole;
}

then in your activity/fragment

yourViewModel.getuserRole.observe(getViewLifecycleOwner(), new new Observer<String>(){
@Overrride
public void onChanged(String s){
//Log.d(TAG, "onChanged: your user role: "+s);
  }
});

The second is making an interface callback

public interface UserRoleInterface(){
  void userRole(String role);
}

Then set the interface as a parameter on your method/function

public void userRole(UserRoleInterface userRoleInterface){
 documentReference = firebaseFirestore.collection("Users").document(firebaseAuth.getCurrentUser().getUid());
 documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
  @Override
  public void onSuccess(DocumentSnapshot documentSnapshot) {
  userRoleInterface.userRole(documentSnapshot.getString("userRole"));
 }
});

Then in your Activity/Fragment

yourViewModel.userRole(new UserRoleInterface(){
@Override
public void userRole(String role){
//Log.d(TAG, "onChanged: your user role: "+role);
}
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文