我可以查看几个复选框,也可以在点击按钮之前取消选中它们 我有一张桌子,我在其中插入我点击的复选框 问题是我还要插入一个取消选中的那个 这是我的代码:
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()));
}
}
怎么办?
答案 0 :(得分:2)
你有两个问题。
所以你会有类似的东西:
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
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());
}
});