0

我正在尝试让文件管理器从 sdcard 中删除文件。只有一个路径“/sdcard/Soundcast/”,我希望能够从中删除 .mp4 和 .3gp 文件。这是我的代码:

公共类 MainActivity 扩展 ListActivity {

private List<String> items =null;

private void deleteFile()
{
    // TODO: Implement this method
}

@Override
public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.main);

    getFiles(new File("/sdcard/Soundcast").listFiles()); }
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{ int selectedRow = (int)id; if (selectedRow == 0)
    { getFiles(new File("/sdcard/Soundcast").listFiles()); }
    else
    { File file = new File(items.get(selectedRow)); if (file.isDirectory())
        { getFiles(file.listFiles()); }
        else
        {
            new AlertDialog.Builder(this) .setTitle("Ability to open files will come in future updates.")
            .setMessage("Do You Want To Delete this recording?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int button){
                    deleteFile();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int button){
                }
            }).show();}
            }
            }
private void getFiles(File[] files)
{ items = new ArrayList<String>();

    items.add(getString(R.string.goto_root)); for (File file : files)
    { items.add(file.getPath()); }

    ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.file_list_row, items);
    setListAdapter(fileList); } }
4

2 回答 2

0

正如 Waza_Be 所述,您所需要的只是 file.delete()。因此,只需删除您的 deleteFile 方法,而不是调用它(在肯定对话框按钮的 onClick 方法中),只需调用 file.delete() 即可:

public void onClick(DialogInterface dialog, int button) {
    // deleteFile();     <-- not needed
    file.delete();   //  <-- do this instead
}
于 2013-06-03T18:59:44.083 回答
0

您应该使用该代码或类似的代码:

{ File file = new File(items.get(selectedRow)); if (file.isDirectory())
    { getFiles(file.listFiles()); }
    else
    {
        new AlertDialog.Builder(this) .setTitle("Ability to open files will come in future updates.")
        .setMessage("Do You Want To Delete this recording?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int button){
                booolean success = file.delete()
                //Refresh UI and your list, so the deleted file is no more diplayed
            }
        })

并且通过扩展过滤并不是很复杂..

我不明白你的问题在哪里。。

于 2013-06-02T15:43:37.593 回答