0

I have a GridView to show images and thier names from sdcard But i want to sort images by names from A to Z This is my code

if (file.isDirectory()) {
        listFile = file.listFiles();
        // Create a String array for FilePathStrings
        FilePathStrings = new String[listFile.length];
        // Create a String array for FileNameStrings
        FileNameStrings = new String[listFile.length];

        for (int i = 0; i < listFile.length; i++) {
            // Get the path of the image file
            FilePathStrings[i] = listFile[i].getAbsolutePath();
            // Get the name image file
            FileNameStrings[i] = listFile[i].getName();

        }
    }

    // Locate the GridView in gridview_main.xml
    grid = (GridView) findViewById(R.id.cgridview);
    // Pass String arrays to LazyAdapter Class
    adapter = new CGridViewAdapter(this, FilePathStrings, FileNameStrings);
    // Set the LazyAdapter to the GridView
    grid.setAdapter(adapter);

How i can sort them?

1 Answers1

0

if (file.isDirectory()) { listFile = file.listFiles();

        ArrayList<EachObject> list = new ArrayList<>();
        for (int i = 0; i < listFile.length; i++) {
            // Get the path of the image file
            FilePathStrings[i] = listFile[i].getAbsolutePath();
            // Get the name image file
            FileNameStrings[i] = listFile[i].getName();

            EachObject object =new EachObject();
            object.setPath(listFile[i].getAbsolutePath());
            // Get the name image file
            object.setName(listFile[i].getName());
            list.add(eachObj);
        }
    }

    grid = (GridView) findViewById(R.id.cgridview);
    // Pass String arrays to LazyAdapter Class
    adapter = new CGridViewAdapter(this, list);//pass one list which have both name //and path and change accordingly in adapter also
    // Set the LazyAdapter to the GridView
    grid.setAdapter(adapter);

public class EachObject {

    public String path;
    public String name;

    public String getPath() {
        return this.path;
    }

    public void setPath(final String path) {
        this.path = path;
    }

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

}