简体   繁体   English

在数组中获取选中的复选框数据

[英]Getting checked Checkbox data in array

I have an android app where I display a form with some Checkboxes.I want to query firestore on the basis of checked checkboxes.我有一个 android 应用程序,我在其中显示带有一些复选框的表单。我想根据选中的复选框查询 firestore。 I'm unable to get the array/arraylist of checked checkboxes.我无法获得选中复选框的数组/数组列表。 For testing purposes I've added a textview.出于测试目的,我添加了 textview。 If my test works I want to set the text of textview using members of newly populated array/arraylist.如果我的测试有效,我想使用新填充的数组/数组列表的成员设置 textview 的文本。

When I check any checkbox and click on the button,my app crashes.当我选中任何复选框并单击按钮时,我的应用程序崩溃了。 Suppose I checked box4.假设我选中了 box4。 The condition (item4.isChecked) should be true and hence item4 must be added to my arraylist whose size is now not equal to zero条件 (item4.isChecked) 应该为真,因此 item4 必须添加到我的 arraylist 中,其大小现在不等于零

if(item4.isChecked()){
            testingArrayList.add("item4");
        }

Why am I getting the error that length of array is zero?为什么我收到数组长度为零的错误?

Here is my code:这是我的代码:

package com.example.XX;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FieldPath;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

public class todayQuestionFragment extends Fragment {

    FirebaseFirestore db = FirebaseFirestore.getInstance();

    CheckBox item1;
    CheckBox item2;
    CheckBox item3;
    CheckBox item4;
    CheckBox item5;
    CheckBox item6;
    CheckBox item7;
    CheckBox item8;
    CheckBox item9;
    CheckBox item10;
    Button button;
    ProgressBar progressBar;

    TextView textView;//just for testing




    public todayQuestionFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_today_question, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        item1=view.findViewById(R.id.checkBox_item1);
        item2=view.findViewById(R.id.checkBox_item2);
        item3=view.findViewById(R.id.checkBox_item3);
        item4=view.findViewById(R.id.checkBox_item4);
        item5=view.findViewById(R.id.checkBox_item5);
        item6=view.findViewById(R.id.checkBox_item6);
        item7=view.findViewById(R.id.checkBox_item7);
        item8=view.findViewById(R.id.checkBox_item8);
        item9=view.findViewById(R.id.checkBox_item9);
        item10=view.findViewById(R.id.checkBox_item10);
        textView=view.findViewById(R.id.testingCheckBox);


        Button button=view.findViewById(R.id.toMainQueryButton);
        final NavController navController= Navigation.findNavController(view);



        final ArrayList<String> testingArrayList = new ArrayList<String>();


        if(item1.isChecked()){
            testingArrayList.add("item1");
        }
        if(item2.isChecked()){
            testingArrayList.add("item2");
        }if(item3.isChecked()){
            testingArrayList.add("item3");
        }if(item4.isChecked()){
            testingArrayList.add("item4");
        }
        if(item5.isChecked()){
            testingArrayList.add("item5");
        }
        if(item6.isChecked()){
            testingArrayList.add("item6");
        }
        if(item7.isChecked()){
            testingArrayList.add("item7");
        }if(item8.isChecked()){
            testingArrayList.add("item8");
        }if(item9.isChecked()){
            testingArrayList.add("item9");
        }
        if(item10.isChecked()){
            testingArrayList.add("item10");
        }

         final String[] testArray = new String[testingArrayList.size()];
        for(int j =0;j<testingArrayList.size();j++){
            testArray[j] = testingArrayList.get(j);
        }


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                textView.setText(testArray[0]);

            }
        });
    }

}

Logcat:日志猫:

2020-06-12 14:48:10.772 4622-4622/com.example.XX E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.XX, PID: 4622
    java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
        at com.example.XX.todayQuestionFragment$1.onClick(todayQuestionFragment.java:140)
        at android.view.View.performClick(View.java:7201)
        at android.view.View.performClickInternal(View.java:7170)
        at android.view.View.access$3500(View.java:806)
        at android.view.View$PerformClick.run(View.java:27582)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7710)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Found my error.发现我的错误。 All my logic with if conditions and arraylist/array needs to be added INSIDE the onClickListener of my button我所有带有 if 条件和 arraylist/array 的逻辑都需要添加到我的按钮的 onClickListener 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM