删除ListView项目行

时间:2014-02-11 10:58:59

标签: android listview

我已经编写代码从SD卡删除图片,我的代码工作正常, 如果我想删除整行,而不仅仅是图片

实际上列表视图的每一行都包含一个带有两个按钮的图像,因此每当我点击删除按钮时,它只是删除不删除该特定行的图像。

final ImageButton btnDelete = (ImageButton) convertView.findViewById(R.id.btnDelete);
        btnDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                fileName = ImageList.get(position).toString().substring(strPath.lastIndexOf('/')+1, strPath.length());
                 String fileToDelete = Environment.getExternalStorageDirectory().getPath() + fileName;
                 Log.d("FileToDelete", fileToDelete);
                  File myFile = new File(fileToDelete);
                      if(myFile.exists())
                        myFile.delete();
                 ((BaseAdapter) lstView.getAdapter()).notifyDataSetChanged(); 
            }
        });

getView(...)

        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

         // If this item is to be synced
            if(flags.get(position)) {
                startUpload(position);

            // Mark as synced
            flags.put(position, false);

        }

1 个答案:

答案 0 :(得分:2)

您需要获得绑定到该列表视图的视图。你可以使用位置变量获得该视图。并删除

myList.remove(position);

然后拨打adapter.notifyDataSetChanged();