如何获取只有目录android的列表

时间:2014-09-05 10:53:56

标签: android listview

我有很好的工作安卓列表,我只需要显示目录。 我尝试了一些方法,但它没有做任何事情。 使用isDirectory()方法,我想我不是那样使用它。

 private String path;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.menu);

                path = Environment.getExternalStorageDirectory().toString();
                if (getIntent().hasExtra("path")) {
                    path = getIntent().getStringExtra("path");
                }

                // Read all files sorted into the values-array
                List<String> values = new ArrayList();
                File dir = new File(path);
                if (!dir.canRead()) {
                    setTitle(getTitle() + " (inaccessible)");
                }

                String[] list = {};;
                if (new File(path).isDirectory() == true) {
                list = dir.list();
                } else {return;}
                if (list != null) {
                    for (String file : list) {
                        if ( new File(path).isDirectory() == true) { // !file.startsWith(".") &&
                            values.add(file);
                        } else  {return;}
                    }
                }
                Collections.sort(values);
                // Put the data into the list
                ArrayAdapter adapter = new ArrayAdapter(this,
                        android.R.layout.simple_list_item_2, android.R.id.text1, values);
                setListAdapter(adapter);
            }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

File f = new File(path);

//list files
File[] filesList = f.listFiles(); //Returns an array of files contained in the directory represented by this file.

//iterate list of files
for (File inFile : files) {
    if (inFile.isDirectory()) {
        // Check is directory
    }
}