在notifyDataSetChanged()之后GridView没有更新

时间:2017-08-06 07:24:57

标签: android gridview notifydatasetchanged

GridView在config.Routes.MapHttpRoute( name: "ControllerAndAction", routeTemplate: "{*uri}/api/{controller}/{action}" ); 之后没有更新 叫

我有一个片段,它有一个GridView,显示来自特定文件夹的图像,notifyDataSetChanged()它需要一个新的onclick,其中文件被删除,广播被发送以更新GridView。 我已经尝试了Activity但仍然没有更新GridView,实际上删除了文件并使用

更新了媒体库
notifyDataSetChanged()

String path = newString;
File myFile = new File(path);
boolean deleted = myFile.delete();
ContentResolver resolver = getContentResolver();
deleteFileFromMediaStore(resolver, myFile);

这是我正在尝试的代码

public void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
        String canonicalPath;

            try {
                canonicalPath = file.getCanonicalPath();
            } catch (IOException e) {
                canonicalPath = file.getAbsolutePath();
            }
            final Uri uri = MediaStore.Files.getContentUri("external");
            final int result = contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?", new String[] {canonicalPath});
            if (result == 0) {
                final String absolutePath = file.getAbsolutePath();
                if (!absolutePath.equals(canonicalPath)) {
                    contentResolver.delete(uri,
                            MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
                }
            }

    }

2 个答案:

答案 0 :(得分:1)

您在设置之前通知适配器,因此您无法看到更改

public void ReloadImages(){
        ImageAdapter  adapteraa = new ImageAdapter(getActivity());
        sdcardImages.setAdapter(adapteraa);
        adapteraa.notifyDataSetChanged();
    }

第二个Cursor仍需要使用相同的数据将其重新加载到ReloadImages方法

答案 1 :(得分:1)

您已经在onActivityCreated方法中为您的gridview设置了一个adepter。您无需在reloadImage()方法中重新初始化它。只要它是空的,只需将其全局化并重新初始化即可。如果它不为null,则只需调用notifyDataSetChanged。

public void ReloadImages(){
              if(adapteraa == null){
                 adapteraa = new ImageAdapter(getActivity());
                 sdcardImages.setAdapter(adapteraa);
              } else{
                adapteraa.notifyDataSetChanged();
             }
    }

永远不要调用通知方法,因为将adpter设置为gridview。