I am trying to delete the items/files in a grid view. The files are located in the /data/my-image-folder/
. The files gets deleted at once but the UI doesn't gets updated instantly.
Here is my code for that:
imageFilePath = /data/<my-image-folder>/"file.jpg";
File file = new File(imageFilePath);
if (file.exists()) {
boolean deleted = file.delete();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(imageFilePath+ Environment.getExternalStorageDirectory())));
getview code :
View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from gridlayout.xml
gridView = inflater.inflate(R.layout.gridlayout, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(fileList[position]);
System.out.println("value of fileList[position]" + fileList[0]);
// set image
ImageView imageThumbnail = (ImageView) gridView
.findViewById(R.id.grid_item_image);
byte[] imageData = null;
try {
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(FILE_PATH
+ fileList[position]);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
Float width = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width / height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap,
(int) (THUMBNAIL_SIZE * ratio), THUMBNAIL_SIZE,
false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
imageThumbnail.setImageBitmap(imageBitmap);
} catch (Exception ex) {
}
} else {
gridView = (View) convertView;
}
return gridView;
}
here is my Code for grid View adapter :
ImageAdapter adapter = null; //ImageAdapter extends BaseAdapter
if (fileList != null) {
adapter = new ImageAdapter(this, fileList);
gridView.setAdapter(i);
}
while after deleting if I do :
adapter.notifyDataChanged();
grid.invalidateViews();
compiler complains to make the adpater as the "final" but if i make it "final" , it complains that i cant make adapter final as it will not allow the below line to happen :
adapter = new ImageAdapter(this, fileList);
Moreover I couldn't find the implementation notifyDataChanged .There is notifyDataSetChanged,notify, notifyAll and notifyDataSetInvalidated but no notifyDataChanged.
I think Intent.ACTION_MEDIA_MOUNTED is for all the images/files located on the external sd card and not for the phone filesystem.is that correct.
How can I achieve it. Plz advice.
Rgds, Softy
You should look at :
adapter.notifyDataSetChanged();
grid.invalidateViews();
It will notified the adapter that something changed, and redraw the grid..
Concerning ,ACTION_MEDIA_MOUNTED the documentation says : External media is present and mounted at its mount point. The path to the mount point for the removed media is contained in the Intent.mData field. The Intent contains an extra with name "read-only" and Boolean value to indicate if the media was mounted read only.
http://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_MOUNTED
So I guess you're right on this
Ok I found the solution : i am updating the fileList again, after the deletion like this
File imageFiles = new File(PATH_TO_IMAGES);
if (imageFiles.isDirectory())
{
fileList = imageFiles.list();
}
then updating the gridview again with the Adapter instance and the filelist like this ->
gridView.setAdapter(new ImageAdapter( ImageGallary.this, fileList));
You dont need to recreate the Adapter. just do so,
File imageFiles = new File(PATH_TO_IMAGES);
if(imageFiles.isDirectory()){
fileList.clear()
fileList.addAll(imageFiles.list());
adapter.notifyDataSetChanged();
}
For more see Android: notifyDataSetChanged(); not working
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With