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);
}
我不知道如果我用正确的方式做到这一点。我想将每个选中的复选框的名称添加到类或字符串中。
您在哪里使用复选框是我的复选框? CheckBox txtExtrasCheckBox = row.FindViewById(Resource.Id.txtExtrasCheckBox); txtExtrasCheckBox.Text = mitems [position] .ExtrasCheckBox; –
Dim
@Dim如果你有一个地方可以通过编程方式添加复选框,那么你应该在创建它们时将它们添加到一个“复选框”列表中。 –
问题是我拿不必要的值。示例double值,当我滚动我的listview.thats我的问题时,三重值。检查我目前的代码看看我更新的答案 – Dim