get a sorted list of directories

This commit is contained in:
tibbi 2016-07-12 22:51:54 +02:00
parent b9063b6126
commit e9c7154a7e
5 changed files with 81 additions and 16 deletions

View File

@ -8,7 +8,7 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@ -16,5 +16,4 @@
</intent-filter>
</activity>
</application>
</manifest>
</manifest>

View File

@ -1,13 +0,0 @@
package com.simplemobiletools.filemanager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

View File

@ -0,0 +1,7 @@
package com.simplemobiletools.filemanager;
public class Utils {
public static String getFilename(final String path) {
return path.substring(path.lastIndexOf("/") + 1);
}
}

View File

@ -0,0 +1,40 @@
package com.simplemobiletools.filemanager.activities;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import com.simplemobiletools.filemanager.R;
import com.simplemobiletools.filemanager.Utils;
import com.simplemobiletools.filemanager.models.Directory;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Directory> dirs = getDirectories();
Collections.sort(dirs);
}
private List<Directory> getDirectories() {
final List<Directory> dirs = new ArrayList<>();
final String path = Environment.getExternalStorageDirectory().toString();
final File root = new File(path);
File[] files = root.listFiles();
for (File file : files) {
if (file.isDirectory()) {
final String dirPath = file.getAbsolutePath();
final String dirName = Utils.getFilename(dirPath);
dirs.add(new Directory(dirPath, dirName));
}
}
return dirs;
}
}

View File

@ -0,0 +1,32 @@
package com.simplemobiletools.filemanager.models;
public class Directory implements Comparable {
private final String path;
private final String name;
public Directory(String path, String name) {
this.path = path;
this.name = name;
}
public String getPath() {
return path;
}
public String getName() {
return name;
}
@Override
public int compareTo(Object object) {
final Directory directory = (Directory) object;
return this.name.compareToIgnoreCase(directory.getName());
}
@Override
public String toString() {
return "Directory {" +
"name=" + getName() +
", path=" + getPath() + "}";
}
}