Android 如何在可展开列表视图中创建一组月份?

Android 如何在可展开列表视图中创建一组月份?,android,expandablelistview,Android,Expandablelistview,如何在可展开列表视图中创建一组月份并为每个月份添加天数?我尝试这样的for循环 private void prepareListData() { months = new ArrayList<String>(); days= new HashMap<String, List<String>>(); daysArray = new ArrayList<String>(); Calendar currentDate =

如何在可展开列表视图中创建一组月份并为每个月份添加天数?我尝试这样的for循环

private void prepareListData() {
    months = new ArrayList<String>();
    days= new HashMap<String, List<String>>();
    daysArray = new ArrayList<String>();

    Calendar currentDate = Calendar.getInstance();

    for (int i = 50; i >= 0; i--) {
        String a = MyChangeDateFomatter.getStringDateFormatMonth(currentDate
                .getTimeInMillis());

        currentDate.add(Calendar.DATE, -1);
        if(months==null||(months.get(i).equalsIgnoreCase(months.get(i-1))==false)) {
            months.add(a);
            for (int j = 50; i >= 0; i--) {
                if(months.get(i).equalsIgnoreCase(months.get(i-1))==true) {
                    String b =  MyChangeDateFomatter.getStringDateFormatMonth(currentDate
                        .getTimeInMillis());
                    daysArray.add(b);
                    days.put(months.get(i),daysArray);
                }
                else {
                    days.put(months.get(i-1),daysArray);
                }
            }
        }   
    }
}
私有void prepareListData(){
月份=新的ArrayList();
days=新建HashMap();
daysArray=newarraylist();
Calendar currentDate=Calendar.getInstance();
对于(int i=50;i>=0;i--){
字符串a=MyChangeDateFomatter.getStringDateFormatMonth(当前日期
.getTimeInMillis());
currentDate.add(Calendar.DATE,-1);
if(months==null | |(months.get(i).equalsIgnoreCase(months.get(i-1))==false)){
增加(a);
对于(int j=50;i>=0;i--){
if(months.get(i).equalsIgnoreCase(months.get(i-1))==true){
字符串b=MyChangeDateFomatter.getStringDateFormatMonth(当前日期
.getTimeInMillis());
daysArray。添加(b);
天.放(月.得(i),天.雷);
}
否则{
天.投入(月.获取(i-1),天.收获);
}
}
}   
}
}
错误

java.lang.RuntimeException:无法启动活动组件信息{com.example.testexpandablelistview/com.example.testexpandablelistview.CalendarActivity}: java.lang.IndexOutOfBoundsException:索引50无效,大小为0


请告诉我怎么做?

您之所以会收到IndexOutOfBoundsException,是因为您试图在没有成员的情况下,在第一个if Station中,在您的月份列表的第50位获取项目

下面的代码应该为您提供正确的月份列表和天数哈希。(不过我还没有测试过,所以可能会有一些错误。)

String a=MyChangeDateFomatter.getStringDateFormatMonth(当前日期
.getTimeInMillis());
增加(a);
daysArray。添加(a);
//删除了一个,因为它是在循环之外添加的
对于(inti=49;i>=0;i--){
当前日期.添加(日历日期,-1);
a=MyChangeDateFomatter.getStringDateFormatMonth(当前日期)
.getTimeInMillis());
//检查a是否在月份列表中
如果(!months.get(months.size()-1).等于(a)){
days.put(months.get(months.size()-1),daysArray);
daysArray=newarraylist();
增加(a);
}
daysArray。添加(a);
}
days.put(months.get(months.size()-1),daysArray);

wow!几乎完美(y)你摇滚!但仍然有一个小问题,所有月份只有14天(从30日到14日);)在尝试删除daysArray.clear()之后,我发现这14天是列表中最后一个月的天数,但我仍然不知道如何修复它:(@Jerrybb所以上个月的天数更少?这是故意的。代码只得到最后51天(包括今天)。您可以在for循环中任意调整迭代次数,以获得更多或更少的结果,这不会影响代码的其余部分。不,我的意思是所有月份(6月、5月、4月)只有14天(4月的几天)-不是他们的日子。是的,这就是我所说的。您太棒了!!:D
    String a = MyChangeDateFomatter.getStringDateFormatMonth(currentDate
            .getTimeInMillis());
    months.add(a);
    daysArray.add(a);

    // Removed one since it was added outside the loop
    for (int i = 49; i >= 0; i--) { 
        currentDate.add(Calendar.DATE, -1);
        a = MyChangeDateFomatter.getStringDateFormatMonth(currentDate
                .getTimeInMillis());

        // Checks if a is in the months List
        if (!months.get(months.size() - 1).equals(a)) {
            days.put(months.get(months.size() - 1), daysArray);
            daysArray = new ArrayList<>();
            months.add(a);
        }
        daysArray.add(a);
    }

    days.put(months.get(months.size() - 1), daysArray);