首页 > 解决方案 > 使用java将多个复选框值插入firestore

问题描述

我是 java 和 Firestore 的新手。我在将多个复选框值插入云 Firestore 中的数组时遇到问题。当我运行代码时,只有最后一个检查的项目作为 pet[0] 存储在 pet 字段中。如何存储所有已检查的项目,而不仅仅是最后一个?

这是我的 pet.java 文件。

    ArrayList<String> arrayPet = new ArrayList<String>();
    ArrayList<String> arrayService = new ArrayList<String>();

         if(cat.isChecked()){
                arrayPet.add("Cat");
            }
            if(dog.isChecked()){
                arrayPet.add("Dog");
            }
            if(rabbit.isChecked()){
                arrayPet.add("Rabbit");
            }
            if(hamster.isChecked()){
                arrayPet.add("Hamster");
            }
            if(bird.isChecked()){
                arrayPet.add("Bird");
            }
            if(aqua.isChecked()){
                arrayPet.add("Aquatic");
            }

            final String[] Pet = new String[arrayPet.size()];
            for(int j =0;j<arrayPet.size();j++){
                Pet[j] = arrayPet.get(j);
                user.put("pet", Arrays.asList(Pet[j])) ;
            }

这是我的 pet.xml 文件。

         <CheckBox
            android:id="@+id/cat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="60dp"
            android:layout_marginTop="10dp"
            android:text="Cat"
            android:textColor="@color/title"
            android:textSize="19sp"
            app:buttonTint="@color/button1" />

        <CheckBox
            android:id="@+id/dog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="60dp"
            android:layout_marginTop="10dp"
            android:text="Dog"
            android:textColor="@color/title"
            android:textSize="19sp"
            app:buttonTint="@color/button1" />

        <CheckBox
            android:id="@+id/rabbit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="60dp"
            android:layout_marginTop="10dp"
            android:text="Rabbit"
            android:textColor="@color/title"
            android:textSize="19sp"
            app:buttonTint="@color/button1" />

        <CheckBox
            android:id="@+id/hamster"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="60dp"
            android:layout_marginTop="10dp"
            android:text="Hamster"
            android:textColor="@color/title"
            android:textSize="19sp"
            app:buttonTint="@color/button1" />

        <CheckBox
            android:id="@+id/bird"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="60dp"
            android:layout_marginTop="10dp"
            android:text="Bird"
            android:textColor="@color/title"
            android:textSize="19sp"
            app:buttonTint="@color/button1" />

        <CheckBox
            android:id="@+id/aqua"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="60dp"
            android:layout_marginTop="10dp"
            android:text="Aquatic Pet  (Fish, Tortoise)"
            android:textColor="@color/title"
            android:textSize="19sp"
            app:buttonTint="@color/button1" />

标签: javaandroidcheckboxgoogle-cloud-firestore

解决方案


pet in 的值user.put("pet", ....)每次都被覆盖,所以你需要在循环之后添加它:

final String[] Pet = new String[arrayPet.size()];
for(int j = 0;j<arrayPet.size();j++) {
    Pet[j] = arrayPet.get(j);
}
user.put("pet", Arrays.asList(Pet));

推荐阅读