1

我在下面有这段代码,我希望这样当我在我的 GridView 中长按我的照片时,我可以选择删除。这一切都完美无缺。但是,当我单击删除时,它实际上并没有从我的 GridView 或 SD 卡目录中删除照片。

       switch (item.getItemId()) {
            case R.id.deletePhoto:
                new AlertDialog.Builder(getActivity()).setTitle("Delete Photo")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        photoGrid.setSelection(i);
                        final File dir = new File(GridViewDemo_ImagePath);
                        dir.mkdirs();
                        File[] files = dir.listFiles(new FilenameFilter() {

                            @Override
                            public boolean accept(File arg0, String arg1) {
                                return arg1.contains(".png");
                            }

                        });

                        File file = files[i].getAbsoluteFile();
                        file.delete();
                        Toast.makeText((MainActivity)getActivity(), "Photo deleted.", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("Cancel", null)
                .create()
                .show();
                break;

有没有人有任何想法如何做到这一点或我在我的代码中做错了什么?任何帮助或示例将不胜感激。提前致谢!

4

2 回答 2

0

你必须把

                    File file = files[i].getAbsoluteFile();

                    file.delete();

里面

  public void onClick(DialogInterface dialogInterface, int i) {}

这样当您在对话框删除照片上按确定按钮时,它将删除照片并调用方法删除;

于 2013-09-17T19:27:37.583 回答
0

你永远不会调用/它永远不会被调用 file.delete();

把它放在对话框的onclick里面

                    File file = files[i].getAbsoluteFile();
                    file.delete();
                    Toast.makeText((MainActivity)getActivity(), "Photo deleted.", Toast.LENGTH_SHORT).show();

.

            case R.id.deletePhoto:
            new AlertDialog.Builder(getActivity()).setTitle("Delete Photo")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    photoGrid.setSelection(i);
                    final File dir = new File(GridViewDemo_ImagePath);
                    dir.mkdirs();
                    File[] files = dir.listFiles(new FilenameFilter() {

                    File file = files[i].getAbsoluteFile();
                    file.delete();
                    Toast.makeText((MainActivity)getActivity(), "Photo deleted.", Toast.LENGTH_SHORT).show();



                    });
               .setNeutralButton("Cancel", null)
            .create()
            .show();
            break;
于 2013-09-17T19:12:58.207 回答