本文介绍了按日期的Android组数组列表项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的节目,我有类节目这样一个数组列表:
In my program I have an array list of type Program like this:
List<Program> programList= new ArrayList<Program>();
public class Program {
public String name;
public String date;
public Program(String name, String date) {
this.name = name;
this.date= date;
}
public String date getDate()
{
return date;
}
public String date setDate(String date)
{
this.date=date;
}
public String date getName()
{
return name;
}
public String date setName(String name)
{
this.name=name;
}
在我的活动我加入项目名单
and in my activity i am adding items to the list
for(int i=0; i < 100; i++)
{
Program p= new Program("name","some date");
programList.add(p):
}
和我想组项目按日期,像有百余项的列表,其中许多人有,我想和日期对具有同一日期的物品的新列表相同的日期。
And I want to group items by date, like there are hundred items in list and many of them have same date, I want to make pairs with the date and the new lists of the items having same date.
推荐答案
首先,你的程序类应该是这样的:
First of all, your Program class should look like this:
private String name;
private String date;
public Program(String name, String date) {
this.name = name;
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
如果您有公共字段,如你有,它的意义,使getter和setter。
If you have public fields like you had, its pointless to make getters and setters.
总之,你可以用HashMap按日期排序的方案:
Anyhow, you could use HashMap for sorting programs by date:
private static class Bucket {
private List<Program> programs = new ArrayList<Program>();
private static HashMap<String, Bucket> map = new HashMap<String, Bucket>();
public static void addProgram(Program p) {
String tempDate = p.getDate();
Bucket correspondingBucket = map.get(tempDate);
if (correspondingBucket == null) {
correspondingBucket = new Bucket();
map.put(tempDate, correspondingBucket);
}
correspondingBucket.programs.add(p);
}
}
这篇关于按日期的Android组数组列表项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!