Search code examples
androidandroid-galleryandroid-cursor

Trying to create a delete button on each image of a gallery


I am trying to create a delete button over each image that appears in a gallery. but when the delete button is pressed on any given image it shows the same uri instead of the one of the image it is holding..
my image adapter:

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

}

Solution

  • Store the URI 0f the image in the tag of the button. Then retrieve it in the onClick.

    The problem with your current code is that uri is declared as a class level field so each time a view is retrieved, it's value is set to the uri of the current image. Since all of your views are retrieved when your layout displays, the uri will always end up as the last view retrieved. You then access it some time later in the onClick handler - which is the same for every button. Thus, every button will access the same value of the uri.

    There are other, perhaps more elegant ways of achieving what you need but storing the uri in the button instance and getting it from the view passed to onClick, which is a button, is easy and simple - but do comment the setTag method call so that you'll know what you did in 6 months time.