在Array中添加选中的CheckBox,从Array中删除未选中的CheckBox

时间:2016-11-13 13:59:51

标签: android listview checkbox

我可以查看几个复选框,也可以在点击按钮之前取消选中它们 我有一张桌子,我在其中插入我点击的复选框 问题是我还要插入一个取消选中的那个 这是我的代码:

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("Gat@gmail.com","GAT"));
    assuranceArray.add(new Assurance("STAR@gmail.com","STAR"));
    assuranceArray.add(new Assurance("Comar@gmail.com","Comar"));
    assuranceArray.add(new Assurance("Ctama@gmail.com","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()));
    }



}

怎么办?

2 个答案:

答案 0 :(得分:2)

你有两个问题。

  • 您永远不会选中复选框状态(已选中或未选中)
  • 您始终在视图点击时向ensureArray添加元素。只有选中复选框后才应添加它。如果取消选中,则从数组中删除元素。

所以你会有类似的东西:

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

答案 1 :(得分:0)

这样,您可以在复选框上从数组添加/删除元素 选中/取消选中是否可以混淆

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

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