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