Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Add convenience methods for sorting and filtering
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Kalderstam <[email protected]>
  • Loading branch information
spacecowboy committed May 30, 2015
1 parent 5d01201 commit 0152b15
Showing 1 changed file with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

import java.io.File;

/**
* An implementation of the picker which allows you to select a file from the internal/external
* storage (SD-card) on a device.
*/
public class FilePickerFragment extends AbstractFilePickerFragment<File> {

public FilePickerFragment() {
Expand Down Expand Up @@ -127,14 +131,7 @@ public SortedList<File> loadInBackground() {
SortedList<File> files = new SortedList<>(File.class, new SortedListAdapterCallback<File>(getDummyAdapter()) {
@Override
public int compare(File lhs, File rhs) {
if (lhs.isDirectory() && !rhs.isDirectory()) {
return -1;
} else if (rhs.isDirectory() && !lhs.isDirectory()) {
return 1;
} else {
return lhs.getName().toLowerCase().compareTo(rhs.getName()
.toLowerCase());
}
return compareFiles(lhs, rhs);
}

@Override
Expand Down Expand Up @@ -240,4 +237,25 @@ protected boolean isItemVisible(final File file) {
return isDir(file) || (mode == MODE_FILE || mode == MODE_FILE_AND_DIR);
}

/**
* Compare two files to determine their relative sort order. This follows the usual
* comparison interface. Override to determine your own custom sort order.
*
* Default behaviour is to place directories before files, but sort them alphabetically
* otherwise.
*
* @param lhs File on the "left-hand side"
* @param rhs File on the "right-hand side"
* @return -1 if if lhs should be placed before rhs, 0 if they are equal,
* and 1 if rhs should be placed before lhs
*/
protected int compareFiles(File lhs, File rhs) {
if (lhs.isDirectory() && !rhs.isDirectory()) {
return -1;
} else if (rhs.isDirectory() && !lhs.isDirectory()) {
return 1;
} else {
return lhs.getName().compareToIgnoreCase(rhs.getName());
}
}
}

0 comments on commit 0152b15

Please sign in to comment.