I'm implementing a simple file browser and I've got navigation working 
(thanks to online sources) now I want to make a home and back button to 
navigate directly to the set root and to the parent directory respectively. 
I made an actionbar and added two buttons but I don't know how to get them 
to function properly. The last function is for setting the button 
functions. Sorry in advance if I'm not posting the code properly The 
following is the code I have (I'm extending ListActivity):



private List<String> item = null;
 private List<String> path = null;
 private String root;
 private TextView myPath;
 private File f;

@Overridepublic boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main, menu);
    return true;}


@Overridepublic 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); }
@Overrideprotected 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();

    }}
@Overridepublic 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);
    }}

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to