我可以選中幾個複選框,也可以在單擊按鈕之前取消選中它們。
我有一個表格,我插入我點擊的複選框。
問題是我也取消選中的那個也被插入。
這是我的代碼:在陣列中添加選中的複選框,從陣列中刪除未選中的複選框
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()));
}
}
怎麼辦?
我剛剛更新了我的問題 –
你試過設置單擊事件偵聽器複選框? https://developer.android.com/guide/topics/ui/controls/checkbox.html –
是的,我的問題是從數組中刪除元素,如果取消選中。 –