使用全局变量从内部函数获取空字符串

请帮我解决一些小问题,我相信你能做到:D

我正在尝试在带有字段“case_number”的 firestore 文档“user_cases_information”上设置一个字段

  1. 首先我声明这个全局变量

private String case_number_consecutive = new String("");

  1. .setOnClickListener我有这个:(从其他文件中读取旧的连续或案例编号)

if (service_type.equals("support")){

    DocumentReference docRef = firestore_popup.collection("param_data_app").document("support_case_numbers");

    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

     @Override

     public void onComplete(@NonNull Task<DocumentSnapshot> task) {

            if (task.isSuccessful()) {

                 DocumentSnapshot document = task.getResult();

                 String consecutive = document.get("consecutive").toString();

                    if (consecutive.equals(null)) {

                         int random = new Random().nextInt(117) + 4;

                         case_number_consecutive = "IOC-09"+random;

                    } else {

                        case_number_consecutive = consecutive;                                        UpdateCaseNumbersConsecutive("support");

                    }

             } else {

                  int random = new Random().nextInt(117) + 4;

                  case_number_consecutive = "IOC-09"+random;

              }

      }

     });



Map<String, Object> user_cases_information = new HashMap<>();

user_cases_information.put("case_number", case_number_consecutive);

... (then i use a firestore reference to put the new case number)


它假设当我检查 firestore 时,正是我设置“user_cases_information”文档的文档,我应该看到数字或新的 case_number,但我总是得到一个参考,连续的字段总是null=null


为什么我得到这个空值?我用了一个 Toast 并把它放在这之前:


Toast.makeText(...."Value: "+case_number_consecutive

Map<String, Object> user_cases_information = new HashMap<>();

user_cases_information.put("case_number", case_number_consecutive);

它显示了null上述函数中的值或数据正在丢失


我怎样才能解决这个问题???万分感谢。


RISEBY
浏览 110回答 2
2回答

元芳怎么了

这样做的原因是因为 firebase 是异步执行的,而您在编码时就好像这一切都是同步发生的一样。这是什么意思 ?这意味着 firebase 调用不会立即从服务器返回值,获取该值需要一些时间,但是在响应返回之前您的其余代码正在执行,这就是您遇到问题的原因。所以,即使这段代码执行:if (service_type.equals("support")){    DocumentReference docRef = firestore_popup.collection("param_data_app").document("support_case_numbers");    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {     @Override     public void onComplete(@NonNull Task<DocumentSnapshot> task) {            if (task.isSuccessful()) {                 DocumentSnapshot document = task.getResult();                 String consecutive = document.get("consecutive").toString();                    if (consecutive.equals(null)) {                         int random = new Random().nextInt(117) + 4;                         case_number_consecutive = "IOC-09"+random;                    } else {                        case_number_consecutive = consecutive;                                        UpdateCaseNumbersConsecutive("support");                    }             } else {                  int random = new Random().nextInt(117) + 4;                  case_number_consecutive = "IOC-09"+random;              }      }     });} 当响应返回时,您的其他代码:Map<String, Object> user_cases_information = new HashMap<>();user_cases_information.put("case_number", case_number_consecutive);已经完成并被调用,然后你永远不会从 firebase 获得实际值。这是你可以做的事情:interface Callback{        void firebaseResponseCallback(String result);//whatever your return type is.    }将此方法的签名更改为如下所示:public void getCaseNumber(Callback callback) { 将您的代码更改为此:if (service_type.equals("support")){    DocumentReference docRef = firestore_popup.collection("param_data_app").document("support_case_numbers");    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {     @Override     public void onComplete(@NonNull Task<DocumentSnapshot> task) {            if (task.isSuccessful()) {                 DocumentSnapshot document = task.getResult();                 String consecutive = document.get("consecutive").toString();                    if (consecutive.equals(null)) {                         int random = new Random().nextInt(117) + 4;                         case_number_consecutive = "IOC-09"+random;                    } else {                        case_number_consecutive = consecutive;                                        UpdateCaseNumbersConsecutive("support");                    }             } else {                  int random = new Random().nextInt(117) + 4;                  case_number_consecutive = "IOC-09"+random;              }       callback.firebaseResponseCallback(case_number_consecutive);      }     });} 然后,当你打电话时getCaseNumber:getCaseNumber(new Callback() {        @Override        public void firebaseResponseCallback(String result) {            here, this result parameter that comes through is your api call result to use, so result will be your case number, so make your objects inside this method, not outside        }    });}

红糖糍粑

您在该代码中犯了一个错误。根据 firebase 最新版本,您只能在 .addOnCompleteListener/.addValueEvent/etc 函数中使用 firebase 数据库的值/字符串。&nbsp; DocumentReference docRef = firestore_popup.collection("param_data_app").document("support_case_numbers");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<DocumentSnapshot> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful()) {/////////////////////////////call here everything related to it////////////////////////&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;在 firebase 外部添加完整监听器或添加值监听器,变量或对象值不会改变或保持不变!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java