取消选中复选框不会从 arraylist 中删除 id

Unchecking checkbox doesn't remove id from arraylist

我正在为一个复选框执行功能,它在选中状态下工作正常,但是当我取消选中该复选框时,它会出错,还有一件事:当我选中该复选框时,我将该项目的 ID 存储在一个数组列表,我想在取消选中该复选框时删除该 ID。

这是错误:

   java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
        at java.util.ArrayList.remove(ArrayList.java:477)
        at com.example.hrtcapp.portalFragments.ListprofileAdapter.onCheckedChanged(ListprofileAdapter.java:56)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:156)
        at android.widget.CompoundButton.toggle(CompoundButton.java:115)
        at android.widget.CompoundButton.performClick(CompoundButton.java:120)
        at android.view.View$PerformClick.run(View.java:22465)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6228)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

代码:

public class ListprofileAdapter extends ArrayAdapter<PROFILE_CALL_POJO>  {
private List<PROFILE_CALL_POJO> heroList;
private Context mCtx;
Button texthideName;
FragmentTransaction fragmentTransaction;

public static ArrayList<String> proUseritems = new ArrayList<String>();

public ListprofileAdapter(ArrayList<PROFILE_CALL_POJO> profile_list, Context context) {
    super(context, R.layout.commonportal_item, profile_list);
    this.heroList = profile_list;
    this.mCtx = context;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    final View listViewItem = inflater.inflate(R.layout.listcustome_item, null, true);
    final TextView textViewName = listViewItem.findViewById(R.id.portal_item);
    final CheckBox checkBox = listViewItem.findViewById(R.id.edit);


    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (buttonView.isChecked() ) {
                ListprofileAdapter.proUseritems.add(String.valueOf(heroList.get(position).getProfile_id(position)));
                Toast.makeText(mCtx, "" + proUseritems.toString(), Toast.LENGTH_SHORT).show();
            }
            else {



                ListprofileAdapter.proUseritems.remove(position);
                    Toast.makeText(mCtx, "" + proUseritems, Toast.LENGTH_SHORT).show();

             }
        }
    });

尝试替换:

proUseritems.remove(position);

与:

proUseritems.remove(String.valueOf(heroList.get(position).getProfile_id(position)))