lundi 31 octobre 2016

When trying to remove from "list" I am getting an ArrayOutOfBoundsException index=-1. Android

I am trying to made an array of checkboxes in a Checkbox container. If the "other" checkbox is selected, then an edittext pops up asking for specification. I also need to grab the value of the checkboxes, which I currently do by grabbing the String value. When I attempt to remove the string value when unchecking a selection I get an out of bounds exception on the array. Where is the logic error in my code? I've been using various questions on here to try to put my program together. Here's the code

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

    }
}




Aucun commentaire:

Enregistrer un commentaire