2017-04-12 44 views
0

我使用自定義listview和複選框我怎樣才能創建一個字符串生成器每次我點擊一個複選框?從每個選中的複選框創建一個帶有textvalue的字符串

這是我customlistiview代碼:

class MyListViewAdapterExtras : BaseAdapter<ExtrasPreviewClass> 
{ 
    public List<ExtrasPreviewClass> mitems; 
    private Context mContext; 
    public MyListViewAdapterExtras(Context context, List<ExtrasPreviewClass> items) 
    { 
     mitems = items; 
     mContext = context; 

    } 
    public override int Count 
    { 
     get 
     { 
      return mitems.Count; 
     } 
    } 
    public override long GetItemId(int position) 
    { 
     return position; 
    } 
    public override ExtrasPreviewClass this[int position] 
    { 
     get 
     { 
      return mitems[position]; 
     } 

    } 

    public override View GetView(int position, View convertView, ViewGroup parent) 
    { 
     string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "student.db3"); 
     var db = new SQLiteConnection(dpPath); 
     db.CreateTable<ExtrasPreviewClass>(); 
     db.DeleteAll<ExtrasPreviewClass>(); 



     View row = convertView; 
     if (row == null) 
     { 
      row = LayoutInflater.From(mContext).Inflate(Resource.Layout.ExtrasPreview, null, false); 

     } 



     TextView txtExtrasName = row.FindViewById<TextView>(Resource.Id.txtExtrasName); 
     txtExtrasName.Text = mitems[position].ExtrasName; 
     CheckBox txtExtrasCheckBox = row.FindViewById<CheckBox>(Resource.Id.txtExtrasCheckBox); 
     txtExtrasCheckBox.Text = mitems[position].ExtrasCheckBox; 
     TextView txtExtrasID = row.FindViewById<TextView>(Resource.Id.txtExtrasID); 
     txtExtrasID.Text = Convert.ToString(mitems[position].id); 
     TextView txtExtrasPrice = row.FindViewById<TextView>(Resource.Id.txtExtrasPrice); 
     txtExtrasPrice.Text = Convert.ToString(mitems[position].ExtrasPrice); 







     txtExtrasCheckBox.Click += delegate 
     { 
      if (mitems[position].IsChecked == true) 
      { 

       Connection.Extras += txtExtrasName.Text + ","; 


      } 


      //in this point i want to add a stringbuilder which it will add all my textvalues which checkbox is checked into a string. 

      //Connection.Extras= "my string" 

      //I dont know if i must use stringbuilder 

     }; 
     //SetOnCheckedChangeListener must be set before txtCheckBox.Checked = mitems[position].IsChecked 
     txtExtrasCheckBox.SetOnCheckedChangeListener(new CheckChangeListener(position, ref mitems)); 
     txtExtrasCheckBox.Checked = mitems[position].IsChecked; 
     Console.WriteLine("Set the CheckBox " + position + " to " + mitems[position].IsChecked); 

     return row; 
    } 

    public class CheckChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener 
    { 
     int position; 
     List<ExtrasPreviewClass> items; 
     public CheckChangeListener(int position, ref List<ExtrasPreviewClass> items) 
     { 
      this.position = position; 
      this.items = items; 
     } 
     public void OnCheckedChanged(CompoundButton buttonView, bool isChecked) 
     { 
      items[position].IsChecked = isChecked; 
      Console.WriteLine("Set the " + position + " IsChecked to " + isChecked); 


     } 

我不知道如果我用正確的方式做到這一點。我想將每個選中的複選框See picture HERE的名稱添加到類或字符串中。

回答

0

點擊一個按鈕什麼的,這意味着用戶完成後,你想是這樣的:

String result = ""; 
for(CheckBox cb : checkboxes) { 
        if (cb.isChecked()){ 
         result += cb.getText() + ", "; 
        } 
       } 

,然後替換用的最後一個逗號「」

+0

您在哪裏使用複選框是我的複選框? CheckBox txtExtrasCheckBox = row.FindViewById (Resource.Id.txtExtrasCheckBox); txtExtrasCheckBox.Text = mitems [position] .ExtrasCheckBox; – Dim

+0

@Dim如果你有一個地方可以通過編程方式添加複選框,那麼你應該在創建它們時將它們添加到一個「複選框」列表中。 –

+0

問題是我拿不必要的值。示例double值,當我滾動我的listview.thats我的問題時,三重值。檢查我目前的代碼看看我更新的答案 – Dim

相關問題