ListView 中点击Item中的Button删除当前行

最近刚好用的去网上找了一下也米找到,现在将我的做法讲一下希望对需要的人有所帮助,我的实现方法是自定义一个适配器然后在适配器里面写删除方法,适配器构造函数AlbumAdapter(Context context,List<File> filelist,int resource),第一个参数上下文对象,第二个是要在listview显示的数据,第三个是listview上子项的布局文件,删除按钮在这个布局文件上写,要删除listview上某一项其实就是删除filelist的某一项,删除方法在适配器的
public View getView( final int position, View convertView, ViewGroup parent){}方法中实现,第一个参数选中对象在集合中的位置,第二个当前显示的条目对象,第三个应该就是宿主对象,
layoutinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//布局填充器 需要在,自定义适配器构造函数中实例化
convertView = layoutinflater.inflate(this.resource, null); //当前显示条目对象,this.resource是布局文件删除按钮在它上面
button_del = (Button) convertView.findViewById(R.id.album_del);
button_del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(file.isDirectory())
{
Handle.delFile(file);
filelist.remove(position); //这个就是删除方法,position就是你选中的子项
AlbumAdapter.this.notifyDataSetChanged(); //记得调用它
}
}
});

原文地址:https://www.cnblogs.com/vicma/p/3484683.html