【问题标题】:Deleting item on arraylist with checkbox使用复选框删除arraylist上的项目
【发布时间】:2019-07-02 00:22:37
【问题描述】:

我正在尝试从名为 arrayofTask 的数组列表中删除一个项目 通过选中一个复选框。我尝试了很多方法来实现这一点,但都没有成功。

访问https://imgur.com/gallery/oRdhpGR,在那里我上传了我的列表视图的图片,我使用添加按钮添加了任务和描述。

我要做的是通过单击复选框删除此类任务和描述

下面是我一直在尝试做的,但我不知道如何进行。

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBox.isChecked()){
                ///i want to delete the arrayoftasks thats located in this location.
                ///tho i am unable to get the location for the tasks in this location.
                ///so if i check the box it should delete the items
            }
        }
    });

这是我的自定义适配器

public class userAdapter extends ArrayAdapter<user> {
    public  userAdapter(Context context, ArrayList<user> users){
        super(context,0,users);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final user User = getItem(position);


        CheckBox checkbox;

        if(convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
        }

        ///this new
        checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
        ///above
        TextView tvTask = (TextView) convertView.findViewById(R.id.task);
        TextView tvDescription = (TextView) convertView.findViewById(R.id.description);

        tvTask.setText(User.task);
        tvDescription.setText(User.description);


        return convertView;
    }


【问题讨论】:

    标签: java android android-studio checkbox


    【解决方案1】:

    我无法打开您的图片,但此代码是为回收站视图编写的,但也适用于列表视图,当单击项目的每个复选框时,项目会从数组列表和回收站视图中删除。
    注意到数组列表。

     public void onBindViewHolder(ViewHolder holder, final int position) {
            //hodler
            holder.txtName.setText(dataObjects.get(position));
    
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        Toast.makeText(CheckActivity.context, "" + position, Toast.LENGTH_SHORT).show();
                        CheckActivity.arrayList.remove(position);
                        CheckActivity.adapter.notifyDataSetChanged();
                    }
                }
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 2015-02-21
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 2010-11-20
      • 2012-11-11
      相关资源
      最近更新 更多