Ben Mustapha Sabrine
Ben Mustapha Sabrine

Reputation: 197

Add checked CheckBox in Array, remove unchecked one from Array

I can check several checkboxes also I can uncheck them before clicking a button.
I have a table in which I insert my clicked checkbox.
The problem is that the one I uncheck are also inserted.
This is my code:

public class InscriptionAssuranceRemorqueur extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

public static ArrayList<Assurance> assuranceArray = new ArrayList<Assurance>();
private ListView list_assurance;
private Button btn_confirmation;
private CheckBox checkbox;

private double largeur;
private double longueur;
private double poids;
private String nom;
private String mail;
private String tel;
private String mdp;
private TextView txt;
ArrayList<Assurance> tab = new ArrayList<Assurance>();
private CheckBox chkIos;

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

    //Intent
    Intent x = this.getIntent();
    nom = x.getExtras().getString("nomCompagnie");
    mail = x.getExtras().getString("mail");
    tel = x.getExtras().getString("tel");
    mdp = x.getExtras().getString("mdp");
    String largeur = x.getExtras().getString("largeur");
    String longueur = x.getExtras().getString("longueur");
    String poids  = x.getExtras().getString("poids");
    this.largeur = Double.parseDouble(largeur);
    this.longueur = Double.parseDouble(longueur);
    this.poids = Double.parseDouble(poids);


    //remplir le tab des compagnies d'assurance
    assuranceArray.add(new Assurance("[email protected]","GAT"));
    assuranceArray.add(new Assurance("[email protected]","STAR"));
    assuranceArray.add(new Assurance("[email protected]","Comar"));
    assuranceArray.add(new Assurance("[email protected]","Ctama"));

    //Récupération

    list_assurance = (ListView)findViewById(R.id.list_assurance);
    btn_confirmation = (Button)findViewById(R.id.btn_cfrm_as_rm);

    //Adapter
    MonAdapter adapter = new MonAdapter(this,assuranceArray);
    list_assurance.setAdapter(adapter);
    list_assurance.setOnItemClickListener(this);

    ArrayList<String> selectedStrings = new ArrayList<String>();

    //Ecouteurs

    btn_confirmation.setOnClickListener(this);


}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


    // When clicked, show a toast with the TextView text
    Assurance a = (Assurance) parent.getItemAtPosition(position);
    Toast.makeText(getApplicationContext(),
            "Clicked on Row: " + a.getNomCompagnie(),
            Toast.LENGTH_LONG).show();


}

@Override
public void onClick(View v) {
    int i=0,j=0;
    Boolean test=true;

    if(v==btn_confirmation)
    {


        while (j<assuranceArray.size())
        {
            Log.d("test",assuranceArray.get(j).getNomCompagnie()+" "+j);
            j++;

        }
    }}



public void itemClicked(View v) {
    //code to check if this checkbox is checked!
    int i=0;



    CheckBox checkBox = (CheckBox)v;

    assuranceArray.add(new Assurance ("email",checkBox.getText().toString()));
    }



}

How to do?

Upvotes: 0

Views: 2368

Answers (2)

Pramila Rawat
Pramila Rawat

Reputation: 1

This way you can add/remove elements from array on checkbox check/uncheck if any confusion you can check

https://www.youtube.com/watch?v=asQbaMaOrEQ&t=153s

OR https://github.com/pamrwt/checkbox_check_uncheck/blob/master/app/src/main/java/o/pam/checkbox_check_uncheck/SpinAdapter.java

label.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        checkedMonthArray.add(names[position]);
                    }else {
                        checkedMonthArray.remove(names[position]);
                    }
                    Log.e("checkedMonthArray",checkedMonthArray.toString());
                }
            });

Upvotes: 0

HelloSadness
HelloSadness

Reputation: 955

You have 2 problems.

  • You never check the checkbox state (checked or not)
  • You always add element to assuranceArray on view click. You should add it only if checkbox got checked. Remove element from array if getting unchecked.

So you will have something like :

if (checkbox.isChecked()) {  
   assuranceArray.add(new Assurance("email",checkBox.getText().toString()));  
} else {  
   for (Assurance assurance : assuranceArray) {  
       if (assurance.getEmail().equals(checkBox.getText().toString())  {  
          assuranceArray.remove(assurance);
          //You can exit the loop as you find a reference
          break;
       }  
   }
}

Upvotes: 2

Related Questions