グリッド ビューでアイテム/ファイルを削除しようとしています。ファイルは/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();
コンパイラは、アダプターを「最終」にするように不平を言いますが、「最終」にすると、以下の行が発生しないため、アダプターを最終にすることができないと不平を言います:
adapter = new ImageAdapter(this, fileList);
さらに、notifyDataChangedの実装が見つかりませんでした。notifyDataSetChanged、notify、notifyAll、およびnotifyDataSetInvalidatedがありますが、 notifyDataChangedがあります。
Intent.ACTION_MEDIA_MOUNTED は、外部 SD カードにあるすべての画像/ファイル用であり、電話のファイルシステム用ではないと思います。それは正しいです。
どうすればそれを達成できますか。アドバイスをお願いします。
Rgds、ソフティ