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

时间:2019-04-25 07:39:11

标签: android checkbox android-studio-3.0

我正在为复选框做功能,并且在检查状态下可以正常工作,但是当我取消选中该复选框时会出现错误,还有一件事:当我选中该复选框时,我会将该项的ID存储在arraylist中并且我想在取消选中该复选框时删除该ID。

这是错误:

   java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
        at java.util.ArrayList.remove(ArrayList.java:477)
        at com.example.hrtcapp.portalFragments.ListprofileAdapter$1.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();

             }
        }
    });

1 个答案:

答案 0 :(得分:0)

尝试更换:

proUseritems.remove(position);

具有:

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