samedi 21 septembre 2019

Checkbox checked list item - Xamarin android

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 need checkbox selected list items

                string[] controlNumbers = DataHolder.GetReRoutedPackages();

            }
            catch (Exception ex)
            {

            }

        }

I added checkbox checked event where I want to get the selected list item details. Here I have no clue how to get the details. I verified numerous articles but I couldn't find the answer. Please help. Thank you.




Aucun commentaire:

Enregistrer un commentaire