lundi 12 décembre 2016

Error to unselect checkboxes

Whenever I click on a checkbox in my main layout an AlertDialog is created that inflates a new layout that contains two checkboxes, after clicking on the setPositive or setNegative button the information is stored in the tot_pos variables.

The problem is when I unselected my checkboxes that were selected, this part is in the last else in which I basically access the checkboxes that were inside the alertDialog corresponding to the checkbox that I want to unselect and as the information I received from it i updated my tot_pos variables. And what it is doing is, it accesses well to the checkbox that I unselected but the information that it will fetch is always the information of the last checkbox that I selected.

In short, if I select the checkbox a then b and then a c and finally d and I want to unselect one of them it will always fetch the information that I entered in the checkbox d no matter the wirch one i select because it was the last checkobx that i select.

public void selectIten(View v){

  checkBoxClicked = (CheckBox)v;
  View ver = ((View)v.getParent());
  id = ver.getResources().getResourceEntryName(ver.getId());


  if(checkBoxClicked.isChecked()){


   AlertDialog.Builder alert = new AlertDialog.Builder(novo_layout_tiro.this, android.R.style.Theme_Holo_Dialog);


   LayoutInflater inflater = this.getLayoutInflater();
   View view = inflater.inflate(R.layout.layout_alert, null);
   //Inicia as variaveis que estão no layout que eu criei
   check_1= (CheckBox)view.findViewById(R.id.check_1);
   check_2= (CheckBox)view.findViewById(R.id.check_2);
   alert.setCancelable(false);
   alert.setView(view);



   alert.setPositiveButton("X", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {

     if(!check_1.isChecked()&&!check_2.isChecked()){
      Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_SHORT).show();
      checkBoxClicked.setChecked(false);
     }
     else{
      if(id.equals("grupo_1")){
       if(check_1.isChecked()){
        tot_pos_1++;
       }
       if(check_2.isChecked()){
        tot_pos_2++;
       }
      }
      if(id.equals("grupo_2")){
       if(check_1.isChecked()){
        tot_pos_1++;
       }
       if(check_2.isChecked()){
        tot_pos_2++;
       }
      }
     }
    }
   });

   alert.setNegativeButton("O", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {

  //Does nothing yet
   });
   alert.show();

  }else{


    if(id.equals("grupo_1")){
     if(check_1.isChecked()){
      tot_pos_1--;
     }
     if(check_2.isChecked()){
      tot_pos_2--;
     }
    }
    if(id.equals("grupo_2")){
     if(check_1.isChecked()){
      tot_pos_1--;
     }
     if(check_2.isChecked()){
      tot_pos_2--;
     }
    }

  }
 }

Aucun commentaire:

Enregistrer un commentaire