嗨,我正在为我正在编写的应用程序的一部分开发一个简单的文件浏览器,但我想做的是,如果文件在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();
}
}
}
}
如果可以依赖文件名,只需检查文件名是否以ZIP扩展名结尾,否则可以检查魔术代码。对于zip文件,它应该是"PK"(0x50 4B)
//--snip
else
{
if (file.getName().toUpperCase().endsWith(".ZIP") ){
//Do something with the zip file
}
}
//--snip
问候
试试这个
file.toString().endsWith(".zip")