【问题标题】:Insert into a class only selected from a customlistive with checkboxes插入仅从带有复选框的自定义列表中选择的类
【发布时间】:2017-04-02 12:43:45
【问题描述】:

我正在使用这个带有复选框的自定义列表视图。

如何获取选中复选框的每一行的名称并将其添加到类中?

这是我的代码:

 SetContentView(Resource.Layout.ExtrasPreviewMain);
        OverridePendingTransition(Resource.Layout.trans_left_in, Resource.Layout.trans_left_out);
        var BackBtn = FindViewById<Button>(Resource.Id.ExtrasBackButton);
        var AcceptBtn = FindViewById<Button>(Resource.Id.ExtrasAcceptButton);
        AcceptBtn.Click += AcceptBtn_Click;



        con.Open();
        SqlCommand sqlCmd = new SqlCommand("SELECT Name from InventoryMaster where MainGroupItemID=" + Connection.CategoryID + "", con);

        mlistview = FindViewById<ListView>(Resource.Id.ExtrasList);
        mItems = new List<ExtrasPreviewClass>();
        SqlDataReader sqlReader = sqlCmd.ExecuteReader();
        while (sqlReader.Read())
        {
            mItems.Add(new ExtrasPreviewClass() { ExtrasName = sqlReader["Name"].ToString() });


        }
        MyListViewAdapterExtras adapter = new MyListViewAdapterExtras(this, mItems);
        mlistview.Adapter = adapter;
        sqlReader.Close();
        con.Close();
    }

    private void AcceptBtn_Click(object sender, EventArgs e)
    {
        var checkbox = FindViewById<CheckBox>(Resource.Id.txtExtrasCheckBox);
        var name = FindViewById<TextView>(Resource.Id.txtExtrasName);


           foreach (var item in mItems)
        {
            if (Convert.ToBoolean(item.ExtrasCheckBox)== true)
            {
                Connection.Extras = Connection.Extras + item.ExtrasName + ",";
            }
        }

但它在我的最后一个结果中没有显示任何内容 例如,如果会有

附加复选框 糖检查 牛奶检查

我的结果将为空

这是我的适配器代码:

  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)
    {
        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 = Convert.ToString(mitems[position].ExtrasCheckBox);



        return row;
    }

还有我的班级

class ExtrasPreviewClass
{
    public string ExtrasName { get; set; }
    public CheckBox ExtrasCheckBox { get; set; }
}

【问题讨论】:

    标签: java c# android xamarin


    【解决方案1】:

    在 Adapter 的 GetView 方法中,您必须为 CheckBox 设置一个 Tag,包含项目信息,并为 CheckBox 的 CheckedChanged 注册一个 EventListener,如下所示:

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        //Prepare convertView here
        CheckBox itemCheckBox = convertView.FindViewById<CheckBox>(Resource.Id.itemCheckBox);
        if(itemCheckBox.Tag == null)
            itemCheckBox.Tag = new Java.Lang.String(this.items[position].name); //set the checkbox tag with information about your item. The tag object class must inherit from Java.Lang.Object
        itemCheckBox.CheckedChange -= itemCheckBox_CheckedChange; //this is to avoid registering the event more than once when scrolling the listview
        itemCheckBox.CheckedChange += itemCheckBox_CheckedChange;
        return convertView;
    }
    
    private void itemCheckBox_CheckedChange(object sender, EventArgs e)
    {
        // CheckBox myCheckBox = (CheckBox)sender;
        // Java.Lang.Object myTag = myCheckBox.Tag;
        // String itemName = ((Java.Lang.String)myTag).ToString();
        //save in a variable that your item was checked
        //for example:
        // foreach(Item i in this.items)
        // {
        //     if(i.name.Equals(itemName))
        //     {
        //         i.IsChecked = myCheckBox.Checked;
        //         break;
        //     }
        // }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多