删除项目(文件)时动态更新网格视图

时间:2012-04-07 19:31:22

标签: android gridview

我正在尝试删除网格视图中的项目/文件。这些文件位于/data/my-image-folder/。文件立即被删除,但UI不会立即更新。 这是我的代码:

   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代码:

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

这是我的网格视图适配器代码:

     ImageAdapter adapter = null; //ImageAdapter extends BaseAdapter
            if (fileList != null) {
                 adapter = new ImageAdapter(this, fileList);
                gridView.setAdapter(i);
            }

删除后如果我这样做:

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

编译器抱怨将adpater设为“最终”,但如果我将其设为“最终”,则会抱怨我无法使适配器最终,因为它不允许以下行发生:

adapter = new ImageAdapter(this, fileList);

此外,我找不到实施 notifyDataChanged notifyDataSetChanged 通知 notifyAll 和< strong> notifyDataSetInvalidated 但没有 notifyDataChanged

我认为Intent.ACTION_MEDIA_MOUNTED适用于位于外部SD卡上的所有图像/文件,而不适用于手机文件系统。这是正确的。

我怎样才能实现它。 Plz建议。

RGDS, 软质皮

3 个答案:

答案 0 :(得分:10)

你应该看看:

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

它会通知适配器有什么变化,并重新绘制网格..

关于, ACTION_MEDIA_MOUNTED 文档说: 外部介质存在并安装在其安装点。已删除介质的装入点的路径包含在Intent.mData字段中。 Intent包含一个名为“read-only”的额外值和一个布尔值,用于指示媒体是否只读。

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

所以我猜你是对的

答案 1 :(得分:1)

好的,我找到了解决方案:我在删除之后再次更新了fileList

File imageFiles = new File(PATH_TO_IMAGES); 

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

然后使用Adapter实例和像这样的文件列表再次更新gridview - &gt;

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

答案 2 :(得分:1)

您不需要重新创建适配器。就这么做,

File imageFiles = new File(PATH_TO_IMAGES); 

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

有关详情,请参阅Android: notifyDataSetChanged(); not working

相关问题