2017-03-16 62 views
1

我用这个代码来删除文件完成任务后如何显示吐司?

File dir = new File(Environment.getExternalStorageDirectory() + "/myapp/" + "/Media/"); 
if (dir.isDirectory()) { 
    String[] children = dir.list(); 
    for (int i = 0; i < children.length; i++) { 
     new File(dir, children[i]).delete(); 

    } 

    Toast.makeText(appActivity.this, "done", Toast.LENGTH_SHORT).show(); 

} 

我想表明吐司“完成”删除文件夹,从和其他吐司“没有文件”的文件后,如果该文件夹中o文件。

我该怎么做?

+0

有啥确切的问题??敬酒不显示? –

+0

toast show ..但是如果文件夹为空,则显示同样的土司“完成”。如果文件夹为空,则显示其他土司如“没有文件” –

+0

检查for循环前的children.length == 0调用“no file”土司其他“完成”敬酒 –

回答

1

嗯,我想这应该

File dir = new File(Environment.getExternalStorageDirectory() + "/myapp/" + "/Media/"); 
if (dir.isDirectory()) { 
    String[] children = dir.list(); 
//if there are files to delete inside the folder 
    if(children.length>0){ 
     Toast.makeText(appActivity.this, "done", Toast.LENGTH_SHORT).show(); 
    } 
//if there are no files inside the folder 
    else{ 
     Toast.makeText(appActivity.this, "No files to delete", Toast.LENGTH_SHORT).show(); 
    } 
    for (int i = 0; i < children.length; i++) { 
     new File(dir, children[i]).delete(); 

    } 



} 
else{ 
    Toast.makeText(appActivity.this, "No Such directory exists", Toast.LENGTH_SHORT).show(); 
} 

希望这有助于

0

你需要的是一个if else语句, 更多的帮助。如果else语句读取的Android开发者文档https://developer.android.com/reference/java/util/concurrent/locks/Condition.html

试试这个;

File dir = new File(Environment.getExternalStorageDirectory() + "/myapp/" + "/Media/"); 
if (dir.isDirectory()) { 
String[] children = dir.list(); 
for (int i = 0; i < children.length; i++) { 
    new File(dir, children[i]).delete(); 

} 

Toast.makeText(appActivity.this, "done", Toast.LENGTH_SHORT).show(); 

else { 
Toast.makeText(appActivity.this, "no file!", Toast.LENGTH_SHORT).show(); 
} 

}