feat: 파일 목록 정렬 기능 추가 (이름순)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package be.gyu.android.file.explorer;
|
||||
|
||||
public class FileItem {
|
||||
public class FileItem implements Comparable<FileItem> {
|
||||
private String name;
|
||||
private String path;
|
||||
private boolean isDirectory;
|
||||
@@ -22,4 +22,16 @@ public class FileItem {
|
||||
public boolean isDirectory() {
|
||||
return isDirectory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(FileItem other) {
|
||||
// Directories first
|
||||
if (this.isDirectory() && !other.isDirectory()) {
|
||||
return -1;
|
||||
} else if (!this.isDirectory() && other.isDirectory()) {
|
||||
return 1;
|
||||
}
|
||||
// Then sort by name, case-insensitively
|
||||
return this.name.compareToIgnoreCase(other.name);
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements FileAdapter.OnItemClickListener {
|
||||
@@ -113,6 +114,7 @@ public class MainActivity extends AppCompatActivity implements FileAdapter.OnIte
|
||||
String fullPath = path.equals("/") ? "/" + file.getName() : path + "/" + file.getName();
|
||||
fileList.add(new FileItem(file.getName(), fullPath, file.isDirectory()));
|
||||
}
|
||||
Collections.sort(fileList);
|
||||
} else {
|
||||
Toast.makeText(this, "Cannot read this directory!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
@@ -154,6 +156,7 @@ public class MainActivity extends AppCompatActivity implements FileAdapter.OnIte
|
||||
for (File file : files) {
|
||||
fileList.add(new FileItem(file.getName(), file.getAbsolutePath(), file.isDirectory()));
|
||||
}
|
||||
Collections.sort(fileList);
|
||||
} else {
|
||||
Toast.makeText(this, "Cannot read this directory!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user