如何在 android 中打开时从列表视图中删除项目?

How to remove items from listview on switch on in android?

我有一个包含带开关按钮的列表视图的应用程序,当开关打开时我想要什么我想从列表视图中删除项目,我已经尝试了很多但无法删除项目。请帮助。

适配器代码:-

private LayoutInflater layoutInflater;
private List<AppList> listStorage;
private Context mContext;
public AppAdapter(Context context, List<AppList> customizedListView) {
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listStorage = customizedListView;
    this.mContext = context;
}

@Override
public int getCount() {
    return listStorage.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder listViewHolder;
    if (convertView == null) {
        listViewHolder = new ViewHolder();
        convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);

        listViewHolder.textInListView = (TextView) convertView.findViewById(R.id.list_app_name);
        listViewHolder.imageInListView = (ImageView) convertView.findViewById(R.id.app_icon);
        listViewHolder.switchCompat = (SwitchCompat) convertView.findViewById(R.id.toggleButton);
        convertView.setTag(listViewHolder);
    } else {
        listViewHolder = (ViewHolder) convertView.getTag();
    }
    listViewHolder.textInListView.setText(listStorage.get(position).getName());
    listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
    listViewHolder.switchCompat.setTag(position);

    listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                listStorage.get(position).getPackName();
                //here i want to remove item from listview 
            } else {
                Toast.makeText(mContext, "Off", Toast.LENGTH_LONG).show();
            }
        }
    });
    return convertView;
}

static class ViewHolder {
    SwitchCompat switchCompat;
    TextView textInListView;
    ImageView imageInListView;
}

}

1.remove 按位置

listStorage.remove(position);
notifyDataSetChanged();
  1. 按对象删除

    listStorage.remove(listStorage.get(position));
    
    notifyDataSetChanged();
    

删除如下所述的元素: 取一个新的List,将删除的元素添加到新的数组中,全局添加它们

List<AppList> newarr = new ArrayList<AppList>();
int newarr_pos = 0;

更新代码

listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // Add your element to new array
                AppList model = datalist0.get(position);
                newarr.add(newarr_pos,model); 
                newarr_pos++;
                //Then remove it from previous array
                listStorage.remove(position);
                notifyDataSetChanged();
                //here i want to remove item from listview 
            } else {
                Toast.makeText(mContext, "Off", Toast.LENGTH_LONG).show();
            }
        }
    });