Update grid view Dynamically when deleting an element (file)

I am trying to delete items / files as a grid. Files are in /data/my-image-folder/. Files are deleted immediately, but the user interface is not updated instantly. Here is my code for this:

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

and after removal, if I do:

adapter.notifyDataChanged();
grid.invalidateViews();

the compiler complains that it makes adpater "final", but if I make it "final", it complains that I cannot make the final version of the adapter, because it will not allow the following line to be executed:

adapter = new ImageAdapter(this, fileList);

, notifyDataChanged. notifyDataSetChanged, , notifyAll notifyDataSetInvalidated, notifyDataChanged.

, Intent.ACTION_MEDIA_MOUNTED /, SD-, . .

. Plz.

Rgds, Softy

+5
3

:

adapter.notifyDataSetChanged();
grid.invalidateViews();

, - , .

ACTION_MEDIA_MOUNTED : . Intent.mData. Intent " " , , .

http://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_MOUNTED

, ,

+10

, : List , ,

File imageFiles = new File(PATH_TO_IMAGES); 

    if (imageFiles.isDirectory())
     {
       fileList = imageFiles.list();
     }

gridview Adapter , →

gridView.setAdapter(new ImageAdapter( ImageGallary.this, fileList)); 
+1

. ,

File imageFiles = new File(PATH_TO_IMAGES); 

if(imageFiles.isDirectory()){
   fileList.clear()
   fileList.addAll(imageFiles.list());
   adapter.notifyDataSetChanged();
}

. Android: notifyDataSetChanged();

+1

All Articles