Ask Your Question
0

How can I use the file chooser in Android to sort files by their suffixes?

asked 2022-07-05 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-10-31 16:00:00 +0000

qstack gravatar image

One way to use the file chooser in Android to sort files by their suffixes is by creating a CustomFilePickerDialog that extends the default FileChooserDialog and overrides its methods.

Here is an example:

  1. Create a CustomFilePickerDialog class that extends the FileChooserDialog and overrides its onFileSelected method:
public class CustomFilePickerDialog extends FileChooserDialog {

   public CustomFilePickerDialog(Context context, File path) {
     super(context, path);
   }

   @Override
   protected void onFileSelected(File file) {
     if (file.isDirectory()) {
        setSelectedFile(file);
     } else {
        File[] files = getFileListAdapter().getFiles();
        Arrays.sort(files, new Comparator<File>() {
           @Override
           public int compare(File file1, File file2) {
              String suffix1 = file1.getName().substring(file1.getName().lastIndexOf(".") + 1);
              String suffix2 = file2.getName().substring(file2.getName().lastIndexOf(".") + 1);
              return suffix1.compareToIgnoreCase(suffix2);
           }
        });
        getFileListAdapter().setFiles(files);
        super.onFileSelected(file);
     }
   }
}
  1. Use CustomFilePickerDialog in your activity:
CustomFilePickerDialog dialog = new CustomFilePickerDialog(this, initialDirectory);
dialog.show();

This will sort the files by their suffixes when a file is selected.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-07-05 11:00:00 +0000

Seen: 9 times

Last updated: Oct 31 '21