我正在使用自定义列表视图和复选框我每次单击一个复选框时如何创建字符串生成器?
这是我的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);
}
答案 0 :(得分:0)
点击按钮或意味着用户完成后,您需要以下内容:
String result = "";
for(CheckBox cb : checkboxes) {
if (cb.isChecked()){
result += cb.getText() + ", ";
}
}
然后用'。'
替换最后一个逗号