mardi 11 octobre 2016

Android Listview item, when I implement setCheck(true) in any checkbox some item changed randomly

this is my first post, and I have a doubt about a listview item management, I have seen some post about recycleView, but I don´t understand yet how to implement it in my problem.

Well, I´ve got a listView that shows some elements like a shopping car where the user clicks the button autorizarDetalleSolicitud, which opens an alertDialog where an user choices a wished amount of that respective item, so if the amount is bigger than zero, the checkBox changes to state TRUE, but when I test this function, I could see that other checkBoxes are activated (or changes to state TRUE) randomly; i.e. When I input an amount to first item, the state of penultimate checkBox changes to TRUE. Thanks for your attention.

/Button of each listview item/

public void autorizarDetalleSolicitud(View v) {


    LinearLayout layoutboton = (LinearLayout) v.getParent();
    checkBoxelemento = (CheckBox) layoutboton.getChildAt(0);

    RelativeLayout relativeLayout = (RelativeLayout) layoutboton.getParent();
    TableLayout tableLayout = (TableLayout) relativeLayout.getChildAt(1);
    TextView tipoelementotextview = (TextView) tableLayout.findViewById(R.id.tipoelem);
    TextView unidadelem = (TextView) tableLayout.findViewById(R.id.unidadelem);
    TextView idelem = (TextView) tableLayout.findViewById(R.id.idelem);
    TextView cantpedida = (TextView) tableLayout.findViewById(R.id.cantidadelem);
    cantautorizadatextview = (TextView) tableLayout.findViewById(R.id.cantautorizado);
    final String tipoelemento = tipoelementotextview.getText().toString();

    cantidadpedida = Double.parseDouble(cantpedida.getText().toString());
    cantidadautorizada = Double.parseDouble(cantautorizadatextview.getText().toString());
    idelemento = idelem.getText().toString();

    // configura el alert dialog
    builder = new AlertDialog.Builder(this);
    builder.setMessage(unidadelem.getText() + " a autorizar:");
    builder.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        }
    });

    if (tipoelemento.equals("Material")) {
        numberPickerCantidad = new NumberPicker(getApplicationContext());
        numberPickerCantidad.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
        numberPickerCantidad.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);


        String[] nums = new String[100];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = Integer.toString(i);
        }
        numberPickerCantidad.setMinValue(0);
        numberPickerCantidad.setMaxValue(nums.length - 1);
        numberPickerCantidad.setWrapSelectorWheel(false);
        numberPickerCantidad.setDisplayedValues(nums);
        if(cantidadautorizada==0){
            numberPickerCantidad.setValue(cantidadpedida.intValue());
        }else{
            numberPickerCantidad.setValue(cantidadautorizada.intValue());
        }

        builder.setPositiveButton(R.string.agregar, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                int cantidadautorizada = numberPickerCantidad.getValue();
                autorizarCantidad((double)cantidadautorizada,tipoelemento);
            }
        });
        builder.setView(numberPickerCantidad);

    } else {
        cantreactivosoli = new EditText(getApplicationContext());
        cantreactivosoli.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        cantreactivosoli.setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});
        cantreactivosoli.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));

        if(cantidadautorizada==0){
            cantreactivosoli.setText(cantidadpedida + "");
        }else{
            cantreactivosoli.setText(cantidadautorizada + "");
        }

        cantreactivosoli.setText(cantidadpedida + "");
        builder.setView(cantreactivosoli);
        builder.setPositiveButton(R.string.aceptar, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                String cantidadedittext = cantreactivosoli.getText().toString();
                if (!cantidadedittext.equals("")) {
                    Double cantidadingresada = Double.parseDouble(cantidadedittext);
                    autorizarCantidad(cantidadingresada,tipoelemento);
                } else {
                    Toast toast = Toast.makeText(getApplicationContext(), R.string.error_sincantidad, Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
        builder.setView(cantreactivosoli);
    }

    builder.show();
}

/Method that saves the input value in an arraylist and implements a setCheck(true) of that itemstrong text/

public void autorizarCantidad(Double cantidadingresada, String tipoelemento){

    if (cantidadingresada <= cantidadpedida) {
        int flag = 0;
        for (int indice = 0; indice < elementosrevisados.size(); indice++) {
            if (elementosrevisados.get(indice).getId().equals(idelemento)) {
                elementosrevisados.get(indice).setCantidadAutorizada(cantidadingresada+"");
                if (tipoelemento.equals("Material")) {
                    cantautorizadatextview.setText(cantidadingresada.intValue()+"");
                }else{
                    cantautorizadatextview.setText(cantidadingresada+"");
                }
                cantidadpedida = cantidadingresada;
                flag=1;
                break;
            }
        }

        if(flag==0) {
            Elemento elem = new Elemento(idelemento, cantidadpedida + "", cantidadingresada + "");
            elementosrevisados.add(elem);
            checkBoxelemento.setChecked(true);
            if (tipoelemento.equals("Material")) {
                cantautorizadatextview.setText(cantidadingresada.intValue()+"");
            }else{
                cantautorizadatextview.setText(cantidadingresada+"");
            }
        }

        for(int i=0;i<elementosrevisados.size();i++){
            Log.e("Elemento revisado: ",elementosrevisados.get(i).toString());
        }
    } else {
        Toast toast = Toast.makeText(getApplicationContext(), R.string.error_cantidadautorizada, Toast.LENGTH_SHORT);
        toast.show();
    }

}




Aucun commentaire:

Enregistrer un commentaire