java - 使用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" />
解决方案
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));
推荐阅读
- scala - 如何将逗号分隔的多列拆分为多行?
- python - 为什么 NumPy 有时比 NumPy + 普通 Python 循环慢?
- sql - 无法加入两个子查询
- postgresql - pg_dumpall | ssh 用户名@server 'cat > alldb.sql'
- python-3.x - Scrapy爬虫在增加spider并发的同时给出DNS查找错误
- python - 如何在 Python 中修复 UserWarning 和 DeprecationWarning
- reactjs - 如何检测刷新页面和路由变化?
- r - 如何修复“doParallel 包的 foreach 循环的 NULL 问题”
- excel - 如何对来自不同行的两个值求和并获得接近某个值的值的组合?
- reactjs - React Native - 在 componentWillUnmount 方法中取消所有订阅和异步任务