samedi 21 novembre 2020

Inserting multiple checkbox values using java into firestore

I am new to java and Firestore. I have issues inserting multiple checkbox value into array in cloud Firestore. When I run the code, only the last item checked is stored as pet[0] in pet field. How can I get all checked items stored and not just the last one?

This is my pet.java file.

    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])) ;
            }

This is my pet.xml file.

         <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" />



Aucun commentaire:

Enregistrer un commentaire