我正在实现一个简单的文件浏览器,并且导航工作正常(感谢在线资源)现在我想制作一个主页和后退按钮来分别直接导航到设置的根目录和父目录。我制作了一个操作栏并添加了两个按钮,但我不知道如何让它们正常工作。最后一个功能是设置按钮功能。如果我没有正确发布代码,请提前抱歉以下是我拥有的代码(我正在扩展 ListActivity):
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
private File f;
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
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];
if(!file.isHidden() && file.canRead()){
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.rows, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.home:
path.add(root);
return true;
case R.id.back:
path.add(f.getParent());
return true;
default:
return super.onOptionsItemSelected(item);
}
}
**代码结束谢谢