Android 排序 ListView

Android Sorting ListView

我有几个 ArrayLists 将进入 ListView 适配器。我想做的是仅按其中之一排序 ArrayLists...

    names = new ArrayList<String>();
    locations = new ArrayList<String>();
    timedates = new ArrayList<String>();

    //arraylists populated here

    adapter = new HomeAdapter(getActivity(), names, locations, timedates);
    setListAdapter(adapter);

例如,我将如何按 timedates 对整个 ListView 进行排序?

感谢任何帮助。谢谢!

试试这个

Collections.sort(timedates,new Comparator<String>(){
    @Override
    public int compare(String s1, String s2){
        return s1.compareTo(s2);
    }
});
adapter.notifyDataSetChanged();

您必须使用只有一个 ArryList 的数据模型。详情如下:

arrayList= new ArrayList<DataModel>();

//arraylists populated here

adapter = new HomeAdapter(getActivity(), arrayList);
setListAdapter(adapter);

DataModel class 将如下所示:

public class DataModel {

    public String names;
    public String locations;
    public String timedates;

}

最后排序:

Collections.sort(arrayList, new SortList());

public class SortList implements Comparator<DataModel> {
    public int compare(DataModel arg0, DataModel arg1) {
        int flag = arg0.timedates.compareTo(arg1.timedates);
        return flag;
    }
}

编辑 1:

要将数据添加到列表中:

DataModel dataModel = new DataModel();
dataModel.names = "Ashiq";
dataModel.locations = "Khulna, Bangladesh";
dataModel.timedates = System.currentTimeMillis(); 

arrayList.add(dataModel);

您不必为每个项目单独使用数组列表。只需将所有字符串包装到dataModel中,并将其添加到arraylist中以添加每组数据。

如果有任何不清楚的地方,请告诉我。