6

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

lambda
  • 3,295
  • 1
  • 26
  • 32
Raulp
  • 7,758
  • 20
  • 93
  • 155

3 Answers3

10

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

Jeremy D
  • 4,787
  • 1
  • 31
  • 38
  • adapter.notifyDataChanged();? I have seen this on most of the SO posts.Can you plz define the adapter .I have this code :gridView.setAdapter(new ImageAdapter(this, fileList)); somewhere on top rgds - softy – Raulp Apr 08 '12 at 14:57
  • I dont understand exactly what you want. The adapter is in fact the link between your files and your gridview so you have to notify the adapter each time you do modification on the items displayed in the gridview. You should paste the code concerning your adapter in the post aswell. – Jeremy D Apr 08 '12 at 15:50
  • what is the code for your getView method? I modified my answer, sorry for that it is notifyDataSetChanged – Jeremy D Apr 08 '12 at 23:11
  • Ok, I will test it tomorrow because i don't really understand :) – Jeremy D Apr 09 '12 at 05:18
  • You should maybe check that, concerning why notifySetDataChanged() could be better than setAdapter() http://stackoverflow.com/questions/2707370/whats-the-best-practice-way-to-update-an-adapters-underlying-data – Jeremy D Apr 10 '12 at 14:19
1

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)); 
Raulp
  • 7,758
  • 20
  • 93
  • 155
1

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

Community
  • 1
  • 1
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256