【发布时间】:2011-09-14 02:22:51
【问题描述】:
您好,我正在为我正在编写的应用程序的一部分开发一个简单的文件浏览器,但我想做的是如果文件在 onclick 中以 .zip 结尾,我想做一些其他的事情到目前为止,这是我的课程,感谢您的帮助
public class Installed extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root= "/";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.installed);
getDir(root);
}
private void getDir(String dirPath)
{
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
Toast.makeText(Installed.this, file.getName() + " can't be read!", Toast.LENGTH_LONG).show();
}
}
else
{
if(file.toString().endsWith(".zip")){
Toast.makeText(Installed.this, file.getName() + " is zip", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(Installed.this, file.getName() + " isn't zip", Toast.LENGTH_LONG).show();
}
}
}
}
【问题讨论】:
-
不到十秒钟就发现了,我没有做 toString,我只是做了 endsWith,这让我对编辑操作感到抱歉
-
是的,但感谢您的回答我会发布我自己的答案,但无论如何我都会给您支票感谢您的帮助:)
标签: java android file browser zip