我想刪除網格視圖中的項目/文件。這些文件位於/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();
c ompiler抱怨使adpater爲「最終」,但如果我讓「最終」,它抱怨說,我不能讓適配器最後,因爲它不會讓下面一行的發生:
adapter = new ImageAdapter(this, fileList);
而且我不能」找不到執行notifyDataChanged。有notifyDataSetChanged,通知,notifyAll的和notifyDataSetInvalidated但沒有notifyDataChanged。
我認爲Intent.ACTION_MEDIA_MOUNTED適用於位於外部SD卡上的所有圖像/文件,不適用於電話filesystem.is。
我該如何實現它。 Plz忠告。
RGDS, 多愁善感
adapter.notifyDataChanged();?我已經在大多數SO帖子中看到了這一點。你可以定義適配器。我有這樣的代碼:gridView.setAdapter(new ImageAdapter(this,fileList));頂部rgds上的某處 - softy –
Raulp
2012-04-08 14:57:39
我完全不明白你想要什麼。該適配器實際上是您的文件和您的gridview之間的鏈接,因此每次您修改gridview中顯示的項目時都必須通知適配器。你應該粘貼有關你的適配器的代碼在帖子中。 – 2012-04-08 15:50:17
Jeremy,更新了問題和gridview代碼。 – Raulp 2012-04-08 18:52:30