java - 按名称对 GridView 中的项目进行排序

标签 java android gridview

我有一个 GridView 来显示 sdcard 中的图像和它们的名称 但我想按名称从 A 到 Z 对图像进行排序 这是我的代码

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);

我如何对它们进行排序?

最佳答案

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)类 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;
    }

}

关于java - 按名称对 GridView 中的项目进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58425874/

相关文章:

c# - ASP.NET 中同一 gridview 行中的多个记录

java - 如何使流返回列表? ( java )

java - 与服务器连接的 AsyncTask

java - 如何在 dsp :switch tags 中获得失败/逻辑或

android - 安卓应用使用谷歌账号登录认证

c# - 如何为未绑定(bind)列单元格设置值? (SetRowCellValue)Grinview Winforms Devexpress

java - JLabel 图标在运行时不改变

安卓 : Failed to resolve attribute at index 13

android - LocationSettings 请求对话框再次出现

c# - GridView 固定标题