2014-09-05 63 views
1

我有很好的工作android列表,我只需要顯示目錄。 我嘗試了一些方法,但它沒有做事。 使用isDirectory()方法,我想我不是那樣使用它。如何獲取唯一的目錄列表android

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

回答

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 
    } 
} 
+0

是啊,我嘗試這樣做,我不'噸明白爲什麼它不'噸我的代碼工作。 – Piarix 2014-09-05 11:24:33

+0

list = dir.list();使用listFiles()方法代替 – deniz 2014-09-05 11:35:06

+0

請調試您的應用程序並找到問題:) – deniz 2014-09-05 11:41:47

相關問題