2012-04-07 5 views
5

Tôi đang cố xóa các mục/tệp trong chế độ xem lưới. Các tệp được đặt trong /data/my-image-folder/. Các tệp bị xóa cùng một lúc nhưng giao diện người dùng không được cập nhật ngay lập tức. Đây là mã của tôi cho rằng:Cập nhật chế độ xem lưới động khi một mục (tệp) bị xóa

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()))); 

đang 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; 
     } 

đây là Mã của tôi cho lưới Xem adapter:

 ImageAdapter adapter = null; //ImageAdapter extends BaseAdapter 
      if (fileList != null) { 
       adapter = new ImageAdapter(this, fileList); 
       gridView.setAdapter(i); 
      } 

thời gian sau khi xóa nếu tôi làm:

adapter.notifyDataChanged(); 
grid.invalidateViews(); 

c ompiler phàn nàn để làm cho adpater là "trận chung kết", nhưng nếu tôi làm cho nó "cuối cùng", nó than phiền rằng tôi không thể làm cho bộ chuyển đổi thức vì nó sẽ không cho phép các dòng dưới đây để xảy ra:

adapter = new ImageAdapter(this, fileList); 

Hơn nữa tôi couldn' t thấy việc thực hiện notifyDataChanged .There là notifyDataSetChanged, thông báo, notifyAllnotifyDataSetInvalidated nhưng không notifyDataChanged.

Tôi nghĩ Intent.ACTION_MEDIA_MOUNTED là dành cho tất cả hình ảnh/tệp nằm trên thẻ sd bên ngoài chứ không phải cho hệ thống tệp của điện thoại.điều đó đúng.

Làm cách nào để đạt được điều đó. Plz tư vấn.

Rgds, softy

Trả lời

10

Bạn nên xem xét:

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

Nó sẽ thông báo cho bộ chuyển đổi điều gì đó thay đổi, và vẽ lại lưới ..

Về, ACTION_MEDIA_MOUNTED tài liệu nói: Phương tiện bên ngoài có mặt và được gắn ở điểm lắp của nó. Đường dẫn đến điểm gắn kết cho phương tiện đã loại bỏ được chứa trong trường Intent.mData. Intent chứa một phần bổ sung có tên "read-only" và giá trị Boolean để chỉ ra nếu phương tiện được gắn kết chỉ đọc.

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

Vì vậy, tôi đoán bạn nói đúng về vấn đề này

+0

adapter.notifyDataChanged() ;? Tôi đã thấy điều này trên hầu hết các bài viết SO.Có thể bạn plz xác định bộ chuyển đổi . Tôi có mã này: gridView.setAdapter (new ImageAdapter (this, fileList)); một nơi nào đó trên đầu trang rgds - softy – Raulp

+0

Tôi không hiểu chính xác những gì bạn muốn. Các bộ chuyển đổi là trong thực tế, liên kết giữa các tập tin của bạn và GridView của bạn, do đó bạn phải thông báo cho bộ chuyển đổi mỗi khi bạn sửa đổi trên các mục hiển thị trong GridView. Bạn nên dán mã liên quan đến bộ điều hợp của bạn trong bài đăng. –

+0

Jeremy, cập nhật câu hỏi và mã GridView. – Raulp

1

Ok Tôi tìm thấy giải pháp: tôi đang cập nhật các filelist một lần nữa, sau khi xóa như thế này

File imageFiles = new File(PATH_TO_IMAGES); 

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

sau đó cập nhật GridView một lần nữa với trường hợp Adapter và filelist như thế này ->

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

Bạn không cần phải tạo lại Bộ điều hợp.chỉ làm như vậy,

File imageFiles = new File(PATH_TO_IMAGES); 

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

Để biết thêm thấy Android: notifyDataSetChanged(); not working