问题描述:

I'm using an expandable list view to display folders and files that are in my app. However, due to my app allowing user to add files, the list would be jumbled up. So, my intention is to set the sorted list (in names) as my child list.

I know that there is Arrays.sort and Collections.sort, but both the method are void, rather then returning the list.

So how do I do it ?

My list:

 private File recordImageFolder = new File (Environment.getExternalStorageDirectory(),"/Record/Image");

private File recordSoundFolder = new File (Environment.getExternalStorageDirectory(),"/Record/Sound");

private String[] groups = { "Image", "Sound" };

private String[][] children = { recordImageFolder.list(), recordSoundFolder.list() };

网友答案:

Instead of initializing these field members in their declarations you could initialize them in the constructor or an init method, allowing you to use Arrays.sort(Object[]):

private File recordImageFolder;
private File recordSoundFolder;

private String[] groups;
private String[][] children;

public MyClass() {

    recordImageFolder = new File (Environment.getExternalStorageDirectory(),"/Record/Image");
    recordSoundFolder = new File (Environment.getExternalStorageDirectory(),"/Record/Sound");

    String[] recordImages = recordImageFolder.list();
    String[] recordSounds = recordSoundFolder.list();

    Arrays.sort(recordImages);
    Arrays.sort(recordSounds);

    groups = new String[] { "Image", "Sound" };
    children = new String[][] { recordImages, recordSounds };
}
相关阅读:
Top