2012-04-07 36 views
5

我想刪除網格視圖中的項目/文件。這些文件位於/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, 多愁善感

回答

10

你應該看看:

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

將通知事情發生了轉變適配器,並重新繪製網格..

關於,ACTION_MEDIA_MOUNTED文檔說: 外部媒體存在並安裝在其安裝點。 Intent.mData字段中包含已移除介質的裝入點路徑。 Intent包含一個額外的名稱爲「只讀」和布爾值,以指示媒體是否已安裝爲只讀。

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

所以我想你是對這個

+0

adapter.notifyDataChanged();?我已經在大多數SO帖子中看到了這一點。你可以定義適配器。我有這樣的代碼:gridView.setAdapter(new ImageAdapter(this,fileList));頂部rgds上的某處 - softy – Raulp 2012-04-08 14:57:39

+0

我完全不明白你想要什麼。該適配器實際上是您的文件和您的gridview之間的鏈接,因此每次您修改gridview中顯示的項目時都必須通知適配器。你應該粘貼有關你的適配器的代碼在帖子中。 – 2012-04-08 15:50:17

+0

Jeremy,更新了問題和gridview代碼。 – Raulp 2012-04-08 18:52:30

1

好,我找到了解決辦法:我又更新的fileList,這樣

File imageFiles = new File(PATH_TO_IMAGES); 

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

刪除後,然後更新gridview再次與Adapter實例和文件列表一樣 - >

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

您不需要重新創建適配器。只是這樣做,

File imageFiles = new File(PATH_TO_IMAGES); 

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

更多看到Android: notifyDataSetChanged(); not working