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

前端之家收集整理的这篇文章主要介绍了android – 删除项目(文件)时动态更新网格视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图删除网格视图中的项目/文件.这些文件位于/ 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,notify,notifyAll和notifyDataSetInvalidated但没有notifyDataChanged.

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

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

RGDS,
多愁善感

解决方法

你应该看看:
adapter.notifyDataSetChanged();
grid.invalidateViews();

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

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

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

所以我猜你是对的

原文链接:https://www.f2er.com/android/310517.html

猜你在找的Android相关文章