当尝试从“列表”中删除时,我得到一个ArrayOutOfBoundsException index = -1。的Android

时间:2016-10-31 18:37:14

标签: java android arraylist checkbox indexoutofboundsexception

我正在尝试在Checkbox容器中创建一个复选框数组。如果选中“其他”复选框,则会弹出一个edittext,要求指定。我还需要获取复选框的值,我可以通过获取String值来完成。  当我在取消选中时尝试删除字符串值时,我在数组上得到一个超出范围的异常。我的代码中的逻辑错误在哪里?我一直在这里使用各种问题来尝试将我的程序放在一起。这是代码

package com.stevenhoule.sportspsychologyquestionaire;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

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


public class DEthnicityActivity extends AppCompatActivity {
    String placeHolder = "NA";
    String ethnicityHolder = "NA";
    public String items = "";

    public String getPlaceHolder() {
        return placeHolder;
    }

    public void setPlaceHolder(String placeHolder) {
        this.placeHolder = placeHolder;
    }

    public String getEthnicityHolder() {
        return ethnicityHolder;
    }

    public void setEthnicityHolder(String ethnicityHolder) {
        this.ethnicityHolder = ethnicityHolder;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ethnicity);

        final List<String> selectedEthnicity = new ArrayList<String>();
        selectedEthnicity.add(getString(R.string.ethnicity0));
        selectedEthnicity.add(getString(R.string.ethnicity1));
        selectedEthnicity.add(getString(R.string.ethnicity2));
        selectedEthnicity.add(getString(R.string.ethnicity3));
        selectedEthnicity.add(getString(R.string.ethnicity4));
        selectedEthnicity.add(getString(R.string.ethnicity5));
        selectedEthnicity.add(getString(R.string.ethnicity6));
        selectedEthnicity.add(getString(R.string.ethnicity7));
        selectedEthnicity.add(getString(R.string.ethnicity8));
        selectedEthnicity.add(getString(R.string.ethnicity9));
        selectedEthnicity.add(getString(R.string.ethnicity10));
        selectedEthnicity.add(getString(R.string.ethnicity11));
        selectedEthnicity.add(getString(R.string.ethnicity12));
        selectedEthnicity.add(getString(R.string.ethnicity13));
        selectedEthnicity.add(getString(R.string.ethnicity14));
        selectedEthnicity.add(getString(R.string.ethnicity15));
        selectedEthnicity.add(getString(R.string.ethnicity16));
        selectedEthnicity.add(getString(R.string.ethnicity17));
        selectedEthnicity.add(getString(R.string.ethnicity18));
        selectedEthnicity.add(getString(R.string.ethnicity19));

        final ArrayList<String> list = new ArrayList<>();
        final ViewGroup checkboxContainer = (ViewGroup) findViewById(R.id.checkbox_container);

        final EditText ethnicityText = (EditText) findViewById(R.id.ethnicity_text);
        ethnicityText.setVisibility(View.INVISIBLE);

        for (int i = 0; i < selectedEthnicity.size(); i++) {
            final CheckBox ethnicityCheckBox = new CheckBox(this);
            String t = selectedEthnicity.get(i);
            ethnicityCheckBox.setText(t);
            checkboxContainer.addView(ethnicityCheckBox);


            ethnicityCheckBox.setOnCheckedChangeListener
                    (new CompoundButton.OnCheckedChangeListener() {
                         @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                         @Override
                         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                             if (isChecked) {
                                 list.add(" " + buttonView.getText().toString());
                             } else {
                                 list.remove(list.indexOf(buttonView.getText().toString()));
                             }

                             //String items = "";
                             for (String item : list) {
                                 items += " " + item;
                                 if (list.contains(getString(R.string.ethnicity19))) {
                                     ethnicityText.setVisibility(View.VISIBLE);
                                     placeHolder = items
                                             + ethnicityText.getText().toString().trim();
                                 } else {
                                     ethnicityText.setVisibility(View.INVISIBLE);
                                     placeHolder = items;
                                 }

                             }
                         }


                     }

                    );
        }
    }

    public void backButton(View view) {
        Intent intent = new Intent(this, CPrimaryLanguageActivity.class);
        startActivity(intent);
    }

    public void nextQuestion(View view) {
        ethnicityHolder = placeHolder;
        String genderHolder = getIntent().getExtras().getString("gender");
        String ageHolder = getIntent().getExtras().getString("age");
        String languageHolder = getIntent().getExtras().getString("language");
        Intent intent = new Intent(this, EMarriageActivity.class);
        intent.putExtra("Gender", genderHolder);
        intent.putExtra("age", ageHolder);
        intent.putExtra("language", languageHolder);
        intent.putExtra("ethnicity", ethnicityHolder);
        startActivity(intent);

    }
}

0 个答案:

没有答案