尝试在图库的每个图像上创建删除按钮

时间:2012-03-30 17:55:08

标签: android android-gallery android-cursor

我正在尝试在图库中显示的每个图片上创建一个删除按钮。但是当在任何给定图像上按下删除按钮时,它会显示相同的uri,而不是它所持有的图像之一 我的图像适配器:

public class GallImageAdapter extends BaseAdapter {
    public Cursor cursor;
    private int columnIndex;
    private Context context;
    int imageBackground;
    String url;
    Uri uri;
    int originalImageId;
    int imageID;
    int columnData;
    ViewGroup myp;

    public GallImageAdapter(Context ctx, Cursor cur, int cIn ) {
        context = ctx;
        columnIndex = cIn;

        cursor = cur;
    }

    @Override
    public int getCount() {

        return cursor.getCount();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        myp = parent;
        View v;
        if(convertView ==null){
            v = LayoutInflater.from(context).inflate(R.layout.galitem, parent, false);
        }else{
            v = convertView;
        }


        ImageView photo = (ImageView) v.findViewById(R.id.imageView);
        ImageView border = (ImageView) v.findViewById(R.id.borderView);
        ImageView d = (ImageView) v.findViewById(R.id.delView);



        // Move cursor to current position
        cursor.moveToPosition(position);

        // Get the current value for the requested column
        imageID = cursor.getInt(columnIndex);
        // obtain the image URI
        uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
        url = uri.toString();
        // Set the content of the image based on the image URI
        originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
        Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
                        originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
        photo.setImageBitmap(b);

        photo.setScaleType(ImageView.ScaleType.FIT_CENTER); 

        d.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub



                Toast.makeText(v.getContext(), "Delete: " + uri + "?", Toast.LENGTH_LONG).show();
                File file = new File(cursor.getString(columnIndex));
                boolean deleted = file.delete();
                //context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                  //       Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
            }


        });

        return v;
    }

}

1 个答案:

答案 0 :(得分:1)

将图像的URI存储在按钮的标记中。然后在onClick中检索它。

当前代码的问题是uri被声明为类级字段,因此每次检索视图时,它的值都设置为当前图像的uri。由于在显示布局时会检索所有视图,因此uri将始终以检索到的最后一个视图结束。然后在onClick处理程序中稍后访问它 - 对于每个按钮都是相同的。因此,每个按钮都将访问uri的相同值。

还有其他可能更优雅的方式来实现你所需要的,但是将uri存储在按钮实例中并从传递给onClick的视图中获取它,这是一个按钮,简单易行 - 但是请注释setTag方法打电话让你知道你在6个月内做了什么。