0

I am new to Android. I have a listview which contains checkbox in each list item. And I have a button under the listview. On button click event I have to capture all the list view items where checkbox is checked.

Here is my Adaptor code.

  class ListAdapter : BaseAdapter<Package>
    {
        List<Package> routeSheet;
        Activity activity;
        public ListAdapter(Activity activity, List<Package> routeSheetRequests)
        {
            this.activity = activity;
            this.routeSheet = routeSheetRequests;
        }

        public override Package this[int position]
        {
            get
            {
                return routeSheet[position];
            }
        }
        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;


            if (view == null)
            {
                view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.CheckoutListRow, 
    parent, false);

                var stopId = view.FindViewById<TextView>(Resource.Id.txtStopId);
                var shipperName = view.FindViewById<TextView>(Resource.Id.txtShipperName);
                var checkBoxMove = view.FindViewById<CheckBox>(Resource.Id.chkCheckout);
                checkBoxMove.CheckedChange += CheckBoxMove_CheckedChange;

                view.Tag = new ListAdapterViewHolder() { StopId = stopId, DeliveryCompany = shipperName, 
            CheckBox = checkBoxMove };
            }
            else
            {

            }
            var holder = (ListAdapterViewHolder)view.Tag;

            holder.StopId.Text = Convert.ToString(routeSheet[position].ControlNumber);
            holder.DeliveryCompany.Text = Convert.ToString(routeSheet[position].DeliveryCompany);

            return view;
        }


        private void CheckBox_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            if (e.IsChecked)
            {
             // Here I want to get the list item details
            }

        }

        public override int Count
        {
            get
            {
                if (routeSheet != null)
                    return routeSheet.Count();
                else
                    return 0;
            }
        }
    }

    class ListAdapterViewHolder : Java.Lang.Object
    {

        public TextView DeliveryCompany { get; set; }
        public TextView StopId { get; set; }
        public CheckBox CheckBox { get; set; }
    }

Here is my Fragment code.

public class CheckoutExcFragment : Android.Support.V4.App.Fragment
    {
        ListView ExcList;
        private List<Package> routesheet = null;

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Package package;
            routesheet = new List<Package>();

                var Packages = Arguments.GetParcelableArrayList("PakageList");
                if (Packages != null)
                {
                    foreach (var pkg in Packages)
                    {
                        PackageParcelable parcelable = (PackageParcelable)pkg;
                        package = parcelable.Package;
                        routesheet.Add(package);
                    }

                }

        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.CheckoutExcFragment, container, false);

            InitListView(view);
            return view;

        }

        public override void OnAttach(Context context)
        {
            base.OnAttach(context);

        }
        private void InitListView(View view)
        {
            ExcList = (ListView)view.FindViewById(Resource.Id.lstCheckoutExc);
            ExcList.ItemClick += ExcList_ItemClick;
            var adapter = new ListAdapter(this.Activity, routesheet);
            ExcList.Adapter = adapter;

        }

        private void ExcList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {

            Package package = routesheet[e.Position];
            if (package !=null)
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(package);
                Intent intent = new Intent(this.Activity, Java.Lang.Class.FromType(typeof(PackageInfo)));
                intent.PutExtra("PackageDetails", json);
                StartActivity(intent);
            }
        }
    }

Main Activity code:

 private void MoveSelectedClick_Click(object sender, EventArgs e)
        {
            try
            {
             // Here I want to get the checkbox selected list items

            }
            catch (Exception ex)
            {

            }

        }
  1. I added checkbox checked event where I want to get the selected list item details and save in a holder.
  2. On button click event get the checked list items from the holder. I'm not sure whether this is the right approach.

My problem is I'm not able to get the checkbox selected list item details. I verified numerous articles but I couldn't find the answer. Please help. Thank you.

1 Answer 1

2

I created a demo to simulate your code, it works properly. The main code is:

CheckBoxAdapter.cs

public class CheckBoxAdapter : BaseAdapter<ItemModel>
{
    private Activity activity;
    private List<ItemModel> datalist;

    public CheckBoxAdapter(Activity activity, List<ItemModel> datalist)
    {
        this.activity = activity;
        this.datalist = datalist;
    }

    public override ItemModel this[int position] {
        get { return datalist[position]; }
    }

    public override int Count {
        get { return datalist.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        MyViewHolder holder;

        if (view != null)
        {
            holder = view.Tag as MyViewHolder;
            holder.mCheckBox.Tag = position;
        }
        else 
        {
            holder = new MyViewHolder();
            view = this.activity.LayoutInflater.Inflate(Resource.Layout.item_layout, null);

            holder.mCheckBox = view.FindViewById<CheckBox>(Resource.Id.chkCaptain);
            holder.DeliveryCompany = view.FindViewById<TextView>(Resource.Id.txtName);
            holder.StopId = view.FindViewById<TextView>(Resource.Id.txtTeam);

            holder.mCheckBox.Tag = position;

            view.Tag = holder;
        }

        ItemModel player = this.datalist[position];

        holder.mCheckBox.SetOnCheckedChangeListener(null);
        holder.mCheckBox.Checked = player.IsChecked;
        holder.mCheckBox.SetOnCheckedChangeListener(new CheckedChangeListener(this.activity,datalist));

        return view;
    }


    public class MyViewHolder : Java.Lang.Object
    {
        public TextView DeliveryCompany { get; set; }
        public CheckBox mCheckBox { get; set; }

        public TextView StopId { get; set; }
    }

    private class CheckedChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener
    {
        private Activity activity;
        private List<ItemModel> list;

        public CheckedChangeListener(Activity activity, List<ItemModel> datalist)
        {
            this.activity = activity;
            this.list = datalist;
        }

        public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
        {
            if (isChecked)
            {
                int position = (int)buttonView.Tag;

                ItemModel item = list[position];
                item.IsChecked = true;
                list[position].IsChecked = true;

                string text = string.Format("{0} Checked.", item.Name);
                Toast.MakeText(this.activity, text, ToastLength.Short).Show();
            }
            else {
                int position = (int)buttonView.Tag;

                list[position].IsChecked= false;
            }
        }
    }
}

When we click the button,then we will get all the checkbox selected list item by filtering the datalist:

private void MBtn_Click(object sender, System.EventArgs e)
    {
        foreach (ItemModel model in dataList) {
            if (model.IsChecked) {
                System.Diagnostics.Debug.WriteLine("selected item = " + model.Name );
            }
        }
    }

Note:

1.we can set and get the position of the item by setting the Tag to the CheckBox.

  if (view != null)
        {
            holder = view.Tag as MyViewHolder;
            holder.mCheckBox.Tag = position;
        }
        else 
        {
            holder = new MyViewHolder();
            view = this.activity.LayoutInflater.Inflate(Resource.Layout.item_layout, null);

            holder.mCheckBox = view.FindViewById<CheckBox>(Resource.Id.chkCaptain);
            holder.DeliveryCompany = view.FindViewById<TextView>(Resource.Id.txtName);
            holder.StopId = view.FindViewById<TextView>(Resource.Id.txtTeam);

            holder.mCheckBox.Tag = position;
            view.Tag = holder;
        }

2.The variable CheckBox of your ListAdapterViewHolder is not right, is a Class Name.

 class ListAdapterViewHolder : Java.Lang.Object
  {
    public TextView DeliveryCompany { get; set; }
    public TextView StopId { get; set; }
    public CheckBox CheckBox { get; set; } // here 
   }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @Jessie Zhang. It's working. You saved my day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.