如何在android的listview滚动中保留复选框状态

时间:2017-07-24 04:58:03

标签: android listview xamarin.android

我有一个学生列表视图,每个行项目都有一个开关和复选框供学生参加。我想在滚动期间保留开关盒和复选框更改。我试过这种方式但是没有成功:

AttendanceCreateNewAdapter.cs有以下部分代码

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

        // re-use an existing view, if one is available
        // otherwise create a new one
        if (view == null)
        {
            view = activityContext.LayoutInflater.Inflate(Resource.Layout.AttendanceCreatenewRowlistLayout, parent, false);
            view.SetTag((Int32)position, view);
        }


        // initialize size for checkbox and switch box states
        checkBoxState = new bool[Count];
        switchBoxState = new bool[Count];

        item = this[position];


            view.FindViewById<TextView>(Resource.Id.student_name).Text = item.StudentName;
            view.FindViewById<TextView>(Resource.Id.date).Text = GetCustomDateTime(item.Date);
            checkBoxLeave = view.FindViewById<CheckBox>(Resource.Id.on_leave);
            switchPresentAbsent = view.FindViewById<Switch>(Resource.Id.present_absent_switch);

            checkBoxLeave.Tag = position;
            switchPresentAbsent.Tag = position;


            checkBoxLeave.SetOnCheckedChangeListener(null);
            switchPresentAbsent.SetOnCheckedChangeListener(null);

            switchBoxState[position] = item.Present;
            checkBoxState[position] = item.Leave;

            checkBoxLeave.Checked = checkBoxState[position];
            switchPresentAbsent.Checked = switchBoxState[position];

  checkBoxLeave.CheckedChange += delegate (object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            CompoundButton buttonView = sender as CompoundButton;
            if (e.IsChecked)
            {

                int itemPosition = (int)buttonView.Tag;
                UpdateAdapterList(itemPosition, false, true);

            }
        };
            return view;
    }
private void UpdateAdapterList(int myPosition, bool leave, bool present)
    {

        NewAttendanceStudent newItem = this[myPosition];

        var attendanceTaken = new NewAttendanceStudent
        {
            StudentName = newItem.StudentName,
            SectionId = newItem.SectionId,
            ClassId = newItem.ClassId,
            RollNumber = newItem.RollNumber,
            Leave = leave,
            Present = present,
            Date = newItem.Date,
        };
        newAttendanceStudentList.Insert(myPosition,attendanceTaken);
    }

谢谢。

2 个答案:

答案 0 :(得分:1)

使用setOnClickListener复选框并更新您已为适配器类传递的全局数据中的复选框状态,并在notifyDataSetChanged();之后调用setOnClickListener。使用复选框数据设置getView中的复选框状态。

// initialized in the getView 

     if (globalData.get(position).getCheckbox().equalsIgnoreCase("1")) {
           holder.checkbox.setChecked(true);
     } else {
           holder.checkbox.setChecked(false);
     }

//checkbox clickelistener
 holder.checkbox.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    final GlobalData clk_row = items.get(position);
                       if (Integer.parseInt(clk_row.getCheckbox()) == 0) {

                              globalData.get(position).setCheckbox("1");

                       } else {

                              globalData.get(position).setCheckbox("0");

                       }


                    notifyDataSetChanged();
                }

            });

答案 1 :(得分:0)

尝试一次这可能会有所帮助,在我的情况下它可行:实现setOnClickListener()方法而不是setOnCheckedChangeListener()

像:

checkBoxSelected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    instrumentClickListener.onCheckBoxClick(instrumentDTO, checkBoxSelected.isChecked(), position);
                    Log.d(TAG, "checked item is check: " + checkBoxSelected.isChecked() + " \nitem: " + position);
                }
            });