【问题标题】:Sort items in GridView by name按名称对 GridView 中的项目进行排序
【发布时间】:2019-10-17 06:02:00
【问题描述】:

我有一个 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);

我如何对它们进行排序?

【问题讨论】:

标签: java android gridview


【解决方案1】:

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

公共类 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;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 2019-01-04
    • 2011-05-04
    • 1970-01-01
    相关资源
    最近更新 更多