1

私のフラグメントには、ファイルとフォルダーのリストがあり、ファイル位置の onclick リスナーがあります。ファイルではなくディレクトリのみを開こうとしていますが、ファイルがディレクトリであってもメソッド isDirectory() は常に false になります。

私のコード

void fileList(String s) {
    currentPath = s;
    items = new ArrayList<String>();
    path = new ArrayList<String>();
    File f = new File(s);
    File[] files = f.listFiles();

    for(int i = 0; i < files.length; i++) {
        File file = files[i];

            if(!file.isHidden() && file.canRead()) {
            path.add(file.getPath());

            if(file.isDirectory()) {
                items.add(file.getName() + "/");
            }

            else {
                items.add(file.getName());
            }
        }
    }
    MyAdapter adapter = new MyAdapter(getActivity(), R.layout.row, items);
    ListView myList = (ListView) view.findViewById(R.id.list);
    myList.setAdapter(adapter);
    myList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            File f = new File(items.get(arg2));
            if(f.isDirectory()) { // <-its always give me false here;
                fileList(path.get(arg2));
            }
            else {
                return;
            }
        }
    });
}
4

1 に答える 1

4
File f = new File(items.get(arg2));
if(f.isDirectory()) { // <-always false because it is a new File...

名前だけでなくフルパスでファイルを作成するか、ディレクトリを指すのではなく、ディレクトリ名で新しい空のファイルを作成します...

File f = new File(path.get(arg2) + "/" + items.get(arg2));
if(f.isDirectory()) { // <-now it will perform the check;

追加のスラッシュが必要かどうかを確認してください。テストされていません。

于 2012-12-11T17:00:50.083 に答える