2016-11-13 90 views
0

我可以選中幾個複選框,也可以在單擊按鈕之前取消選中它們。
我有一個表格,我插入我點擊的複選框。
問題是我也取消選中的那個也被插入。
這是我的代碼:在陣列中添加選中的複選框,從陣列中刪除未選中的複選框

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



} 

怎麼辦?

+0

我剛剛更新了我的問題 –

+0

你試過設置單擊事件偵聽器複選框? https://developer.android.com/guide/topics/ui/controls/checkbox.html –

+0

是的,我的問題是從數組中刪除元素,如果取消選中。 –

回答

2

您有2個問題。

  • 你永遠不選中該複選框狀態(選中與否)
  • 你總是添加元素assuranceArray上視圖點擊。只有在複選框被選中的情況下,您才應該添加它。如果取消選中,則從數組中刪除元素。

所以,你會碰到這樣的:

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

這是我的測試,如果它被檢查:'if(checkBox.isChecked())',如果不是? –

+0

我該如何引用該對象 –

+0

我更新了答案。你應該循環扔陣列。如果您找到具有相同名稱值的保證對象,則將其刪除。 – HelloSadness