Forum Xamarin.Android

Spinners Inside long ListView gets reset after scroll

AbdulMuhayminArifAbdulMuhayminArif INUniversity ✭✭
edited November 2015 in Xamarin.Android

Hi,

I spent more than 3 days learning about the issue, tried many suggestions like using Hashmap (well I tried with Dictionary), with no luck. The issue I am facing is I have a Custom Adapter with 1 TextView, and 2 Spinners, Whenever the list is long, and I selected some values in spinner, and I scrolled to bottom and comes back, the value at beginning which I set is reset. How to solve the issue in C# way.

namespace BunkyAndroid
{
    class AttendanceAdapter : BaseAdapter
    {

        Context context;
        List<Attendance> attendance;
        List<Subjects> subjects;
        List<string> status;
        public AttendanceAdapter(Context context, List<Attendance> attendance, List<Subjects> subjects, List<string> status)
        {
            this.context = context;
            this.attendance = attendance;
            this.subjects = subjects;
            this.status = status;
        }



        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;
            AttendanceAdapterViewHolder holder = null;

            if (view != null)
                holder = view.Tag as AttendanceAdapterViewHolder;
            var currentattendance = attendance[position];
            if (holder == null)
            {
                holder = new AttendanceAdapterViewHolder();
                var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
                //replace with your item and your holder items
                //comment back in

                view = inflater.Inflate(Resource.Menu.attendanceview, parent, false);
                holder.Hour = view.FindViewById<TextView>(Resource.Id.hourview);
                holder.StatusSpinner = view.FindViewById<Spinner>(Resource.Id.attendancespinner);
                holder.SubjectSpinner = view.FindViewById<Spinner>(Resource.Id.subjectspinner);
                holder.SubjectSpinner.Adapter = new SubjectsAdapter(this.context, subjects);
                holder.StatusSpinner.Adapter = new ArrayAdapter(this.context, Android.Resource.Layout.SimpleListItem1, status);
                view.Tag = holder;
            }



            //fill in your items
            holder.Hour.Text = "Hour " + currentattendance.hour;
            holder.Hour.Tag = currentattendance.hour;

            return view;
        }




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

    }

    class AttendanceAdapterViewHolder : Java.Lang.Object
    {
        public TextView Hour { get; set; }
        public Spinner SubjectSpinner { get; set; }
        public Spinner StatusSpinner { get; set; }
    }
}

Answers

  • rzee7rzee7 INUniversity ✭✭✭✭✭

    Hi @AbdulMuhayminArif

    I think issue is that you are not maintaining state of your spinner. you should maintain its state with item or may be position you are selecting, because on every scroll GetView will invoked again and will render list row view again, therefore, you have to maintain your control state. ref Link

    Hope its works for you!

    Cheers!!
    RIYAZ

  • hbakshihbakshi INMember
    edited May 2016

    How to do it in c#. Any Example @AbdulMuhayminArif

  • MizanurRahman.0834MizanurRahman.0834 USMember ✭✭

    Take the the selection item index of a spinner and set that index in ListView scrolling.

Sign In or Register to comment.