Problem with list of options with string-array on Android

0

I am creating a list of options. My problem is when I select and then I want to select that same option. Mark me error in this part:

mMercado.remove(position);

Strings Code:

<string-array name="mercado">
        <item>Nacional</item>
        <item>EEUU</item>
        <item>Canada</item>
        <item>Europa</item>
        <item>Japon</item>
        <item>Organico</item>
        <item>Limpio</item>
        <item>SemíLimpio</item>
        <item>2daClase</item>
        <item>China</item>
        <item>Mendez</item>
        <item>Aventajado</item>
        <item>Bandeado</item>
    </string-array>

Code of the button that launches the list:

bmercado =(Button)findViewById(R.id.btnMercado);
    bmercado.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(Cortes_Registro.this);
            mBuilder.setTitle("Seleccionar Mercado");
            mBuilder.setMultiChoiceItems(listMercado, checkMercado, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int position, boolean isChecked) {
                    if (isChecked){
                        if (! mMercado.contains(position)){
                            mMercado.add(position);
                        }
                    }else if (mMercado.contains(position)){
                        mMercado.remove(position);
                        //Aqui me marca error!!! <<<<<<<<<<<<<<<<<<<<<
                    }
                }
            });

            mBuilder.setCancelable(false);
            mBuilder.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String item = "";
                    for (int i=0; i< mMercado.size(); i++){
                        item = item+listMercado[mMercado.get(i)];
                        if (i != mMercado.size() -1){
                            item = item + ",";
                        }
                    }
                    infoMercado.setText(item);
                }
            });

            mBuilder.setNegativeButton("Cerrar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                }
            });
            mBuilder.setNeutralButton("Limpiar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                for (int i = 0; i<checkMercado.length;i++){
                    checkMercado[i] = false;
                    mMercado.clear();
                    infoMercado.setText("Seleccionar Mercado");
                }
                }
            });
            AlertDialog mDialog = mBuilder.create();
            mDialog.show();
        }
    });

The error:

  

E / AndroidRuntime: FATAL EXCEPTION: main Process:   developersalpha.hassmovil, PID: 5123   java.lang.IndexOutOfBoundsException: Index: 4, Size: 1 at   java.util.ArrayList.remove (ArrayList.java:477) at   developersalpha.hassmovil.Cortes_Confirm $ 4 $ 1.onClick (Cortes_Confirmar.jav a: 94)

    
asked by DoubleM 20.02.2017 в 07:31
source

2 answers

1

The problem is that you are trying to delete item 4 when you only have 1 item:

java.lang.IndexOutOfBoundsException: Index: 4, Size: 1

The problem comes when you make the mMercado.remove(position) since I sense that what you want to do is to delete the object position and not the object that contains the list in the position position . To get what you need, you have to find the position of the object and then delete it with the remove:

int pos = mMercado.indexOf(position);
mMercado.remove(pos);
    
answered by 20.02.2017 / 10:17
source
-1

You are removing an item in index 4 ( position ) when the size of all ArrayList is of only one element:

  

IndexOutOfBoundsException: Index: 4, Size: 1 at   java.util.ArrayList.remove (ArrayList.java:477)

You have to perform a validation, allowing the element to be deleted in the defined position, only if the ArrayList size is greater than position ( position is an index starting at 0):

 if(mMercado.size() > position){
       mMercado.remove(position);
 }

I suggest you check your code since I do not see why you would search if in the ArrayList you contain an integer, and based on this, remove the position related to that integer:

                else if (mMercado.contains(position)){
                    mMercado.remove(position);
                    //Aqui me marca error!!! <<<<<<<<<<<<<<<<<<<<<
                }

Regularly search for an element within the ArrayList and remove / add:

 if (mMercado.contains(mMercado.get(position))){ // Busca en el ArrayList un elemento, para eliminarlo.
     mMercado.remove(position);
 }

The value of the variable position will never be greater since it is related to the number of elements in the UI, it is important to take into account that the initial value of position in the first element is 0 since it is an index.

    
answered by 20.02.2017 в 21:21