android - Delete item from gridview -
i know here many answers how delete items, can't make work. show errors. can look? added in adapter remove(position), think works wrongly. want after using onitemlongclicklistener
delete file , thumbnail too.
main:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final gridview grid = (gridview) findviewbyid(r.id.gridview); final thumbnailadapter thumbnails = new thumbnailadapter(this); grid.setadapter(thumbnails); grid.setonitemlongclicklistener(new adapterview.onitemlongclicklistener(){ @override public boolean onitemlongclick(adapterview<?> parent, view v, int position, long id) { final string imgpath = thumbnails.getimagepath(position); file file = new file(imgpath); file.delete(); thumbnails.remove(position); thumbnails.notifydatasetchanged(); grid.invalidateviews(); grid.setadapter(thumbnails); return true; } });
adapter:
public class thumbnailadapter extends baseadapter { // context required performing queries private final context mcontext; // cursor thumbnails private final cursor cursor; private final int imgid; private final int imgdata; private final int count; public thumbnailadapter(context c) { this.mcontext = c; // list of images, sorted last taken first final string[] projection = { mediastore.images.media._id, mediastore.images.media.data }; cursor = mcontext.getcontentresolver().query( mediastore.images.media.external_content_uri, projection, null, null, mediastore.images.media.date_taken + " desc" ); imgid = cursor.getcolumnindexorthrow(mediastore.images.media._id); imgdata = cursor.getcolumnindexorthrow(mediastore.images.media.data); count = cursor.getcount(); log.d("thumbnailadapter", count + " images found"); } @override public int getcount() { return count; } @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) { imageview imageview; if (convertview == null) { // if it's not recycled, initialize attributes imageview = new imageview(mcontext); } else { imageview = (imageview) convertview; } cursor.movetoposition(position); final bitmap thumbnail = mediastore.images.thumbnails.getthumbnail( mcontext.getcontentresolver(), cursor.getint(imgid), mediastore.images.thumbnails.micro_kind, null ); imageview.setimagebitmap(thumbnail); log.d("thumbnailadapter", "render: " + cursor.getstring(imgdata)); return imageview; } public string getimagepath(int position) { cursor.movetoposition(position); return cursor.getstring(imgdata); } public void remove(int position) { remove(position); notifydatasetchanged(); }
}
i guess stack overflow. remove method in adapter recursive forever. instead of deleting file youself ask content resolver remove it. , reload data. dont have call grid.setadapter(thumbnails) again in click listner
Comments
Post a Comment