0

I've created a file manager that I will use in my other program, however I am having problems with it. It displays a list of files on my android phone, and I can click through one folder deep, however going more than one it stops working. Mainly I think because of "if(K.getAbsoluteFile().isDirectory()==true)", if I get rid of this line though it simply crashes. So it seems to only allow me to go one folder deep, can anyone figure out what I've done wrong here?

Also any guides or what have you on this topic would be appreciated, its kind of a mangling of random code I've found.


package book.BouncySquare;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FileManager extends ListActivity {
    public File clickedFile = null;
    private List<String> directoryEntries = new ArrayList<String>();

    //private List<String> directoryEntries = new ArrayList<String>();
    private File currentDirectory = new File("/");

    @Override
    public void onCreate(Bundle icicle) {
        Log.d("startx", "start");
        super.onCreate(icicle);
        browseToRoot();
    }



    public void browseToRoot() {
        Log.d("Browse", "browse"+currentDirectory.getAbsoluteFile());
        browseTo(new File("/"));
    }



    private void browseTo(File file) {
        Log.d("mew", "too");
        if(file.isDirectory()){
            Log.d("check", "it");
        fill(file);     }
    }


    private void browse(String x) {
        Log.d("created", "newfile");
        final File K=new File(x);   
        Log.d("broke", "ass");
        if(K.getAbsoluteFile().isDirectory()==true){
        Log.d("Z"+K.getAbsoluteFile(), "directory");
        fill(K.getAbsoluteFile());}
        else{
            Log.d("X"+K.getName(), "NotADirectoryOrHidden");
            Log.d("A"+K.getAbsoluteFile(), "directory");
        }

    }



    private void fill(File files) {
        File[] meow=null;
        this.directoryEntries.clear();
        meow= files.listFiles();
        Log.d("sss", "sss");
        this.directoryEntries.add(getString(R.string.current_dir));// directoryentries is an arraylist that holds our Directories names
        this.directoryEntries.add(getString(R.string.up_one_level));
        for (File file : meow) {
            this.directoryEntries.add(file.getName());      //fill our string array directoryentries with each files getName, then we pass it to arrayAdapter to populate
            }
        ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,R.layout.file_row, this.directoryEntries); //our context,layout, and array of directories is created
        this.setListAdapter(directoryList);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String clickedEntry =this.directoryEntries.get((int) id);
        Log.d("clickedID="+id, "clickedPosition="+clickedEntry);    
        browse(clickedEntry);
    }
}
4

1 回答 1

1

尝试将当前目录添加到要列出的内容之前。

private void browse(String x) {
    Log.d("created", "newfile");
    final File K=new File(currentDirectory, x);   
    Log.d("broke", "ass");
    if(K.getAbsoluteFile().isDirectory()==true){
    Log.d("Z"+K.getAbsoluteFile(), "directory");
    currentDirectory = K.getAbsoluteFile();
    fill(K.getAbsoluteFile());}
    else{
        Log.d("X"+K.getName(), "NotADirectoryOrHidden");
        Log.d("A"+K.getAbsoluteFile(), "directory");
    }

}
于 2012-06-24T01:27:50.290 回答