【问题标题】:I want to Retrieve the Boolean Value From firestore all I want is a syntax from which I can again check the Checkbox getting true value from Firestore我想从firestore中检索布尔值我想要的只是一个语法,我可以再次检查复选框从Firestore获取真值
【发布时间】:2021-05-30 15:43:55
【问题描述】:

布尔 wifi; 复选框 cB1;

问题是代码不起作用我从 Toast 获取值更改数据类型时,但我确实需要在从 Firestore 获取“ture”值后再次检查复选框... 期待好的回复

DocumentReference dc = firebaseFirestore.collection("Hostel Admin").document(var);
        dc.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {



                wifi = Boolean.valueOf(value.getString("Wifi"));
                if (wifi.equals("true")) {
                    cB1.setChecked(true);
                } else {
                    cB1.setChecked(false);
                }
            }
        });

【问题讨论】:

    标签: javascript java google-cloud-firestore boolean data-retrieval


    【解决方案1】:

    如果你想使用 Java 在 Firestore 中获取一个名为 wifi 的文档字段,可以运行以下代码:

    DocumentReference dc = firebaseFirestore.collection("Hostel Admin").document(var);
    dc.addSnapshotListener(new EventListener<DocumentSnapshot>() {
      @Override
      public void onEvent(@Nullable DocumentSnapshot snapshot,
                          @Nullable FirestoreException e) {
        if (e != null) {
          System.err.println("Listen failed: " + e);
          return;
        }
        // IF the document exists
        if (snapshot != null && snapshot.exists()) {
          System.out.println("Current data: " + snapshot.getData());
          
          // Get the wifi field of the document
          String document_wifi = snapshot.getString("wifi");
          // Convert the value to boolean
          Boolean wifi1 = Boolean.valueOf(document_wifi);
          boolean wifi = wifi1.booleanValue();
          // Or you can use the boolean primitive type (Uncomment the following line if you want boolean primitive type)
          // boolean wifi = Boolean.parseBoolean(document_wifi);
    
          // Then check if the boolean wifi contains the value true or false
          if (wifi) {
             cB1.setChecked(true);
          } else {
             cB1.setChecked(false);
          }
        } else {
          System.out.print("Current data: null");
        }
      }
    });
    
    

    请查看以下Firebase Documentation

    您的代码无法正常工作,因为您正尝试使用 .equals() 比较字符串的方法检查包含布尔变量的内容。

    请注意,Java 中的布尔类型和布尔类型是有区别的:

        Boolean boolean1 = Boolean.valueOf("true");
        boolean boolean2 = Boolean.parseBoolean("true");
    
    
    • Boolean:Boolean 类将原始类型 boolean 的值包装在一个对象中。布尔类型的对象包含一个布尔类型的字段。

    • boolean:不需要实例,你使用原始类型。

    因此,如果您使用 Boolean.valueof(str) 方法,它将返回一个 Boolean 对象并查看它包含的内容,您需要按照官方文档Javadoc 使用 Boolean 类的 booleanValue() 方法。

    所以如果你想从 String 转换为布尔值,你可以使用带有方法 parseBoolean(str) 的原始类

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2014-08-12
      • 2015-12-11
      • 2021-08-22
      • 1970-01-01
      相关资源
      最近更新 更多