我正在尝试删除 GridView 中的项目/文件。这些文件位于 /data/my-image-folder/
中。文件被立即删除,但用户界面不会立即更新。
这是我的代码:
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())));
获取查看代码:
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;
}
这是我的 GridView 适配器代码:
ImageAdapter adapter = null; //ImageAdapter extends BaseAdapter
if (fileList != null) {
adapter = new ImageAdapter(this, fileList);
gridView.setAdapter(i);
}
如果我删除了之后:
adapter.notifyDataChanged();
grid.invalidateViews();
编译器提示将 adpater 设为“final”,但如果我将其设为“final”,它会提示我无法将适配器设为 final,因为它不允许以下行发生:
adapter = new ImageAdapter(this, fileList);
此外,我找不到实现 notifyDataChanged 。有 notifyDataSetChanged、notify、notifyAll 和 < strong>notifyDataSetInvalidated 但没有 notifyDataChanged。
我认为 Intent.ACTION_MEDIA_MOUNTED 适用于位于外部 SD 卡上的所有图像/文件,而不适用于手机文件系统。是否正确。
我怎样才能实现它。请指教。
Rgds, 软软的
最佳答案
你应该看看:
adapter.notifyDataSetChanged();
grid.invalidateViews();
它会通知适配器发生了某些变化,并重新绘制网格..
关于 ACTION_MEDIA_MOUNTED 文档说: 外部媒体存在并安装在其安装点。已删除媒体的安装点路径包含在 Intent.mData 字段中。 Intent 包含名称为“只读”的额外项和 bool 值,以指示媒体是否以只读方式挂载。
http://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_MOUNTED
所以我想你是对的
关于android - 删除项目(文件)时动态更新 GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10057645/