【问题标题】:How to remove items from listview on switch on in android?如何在android中打开时从listview中删除项目?
【发布时间】:2017-07-10 05:13:22
【问题描述】:

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

适配器代码:-

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;
}

}

【问题讨论】:

  • 从 listStorage 中删除项目:listStorage.get(position).remove();之后调用 notifyDataSetChanged
  • remove() 中显示“无法解析方法”
  • 我正在扩展 BaseAdapter
  • listStorage.remove(位置)。试试这个

标签: android switchcompat


【解决方案1】:

删除元素如下所述: 取一个新的 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();
            }
        }
    });

【讨论】:

  • 感谢南希,它可以工作.....如果我想将要删除的位置项添加到另一个数组列表中,那么我该怎么做
  • 是的,它有效,但在我的评论中为我提供了上述问题的解决方案
  • 嘿,我已经更新了代码。到你的要求。请检查
【解决方案2】:

1.按位置删除

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

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

【讨论】:

  • 这是适配器的名称。 .不管是什么。
猜你喜欢
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 2016-09-24
  • 2011-05-27
  • 2016-12-03
  • 1970-01-01
相关资源
最近更新 更多