如何在可扩展列表视图中创建一组月份?
How can I create a group of months in expandable list view?
如何在可扩展列表视图中创建一组月份并为每个月份添加天数?我尝试像这样的 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);
}
}
}
}
}
错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testexpandablelistview/com.example.testexpandablelistview.CalendarActivity}:
java.lang.IndexOutOfBoundsException: Invalid index 50, size is 0
请教我怎么做?
您收到 IndexOutOfBoundsException 的原因是因为您试图在您的第一个 if 语句中获取月份列表第 50 位的项目,而该列表没有成员。
以下代码应为您提供正确的月份列表和日期哈希。 (虽然我还没有测试过,所以可能会有一些错误。)
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);
如何在可扩展列表视图中创建一组月份并为每个月份添加天数?我尝试像这样的 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);
}
}
}
}
}
错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testexpandablelistview/com.example.testexpandablelistview.CalendarActivity}: java.lang.IndexOutOfBoundsException: Invalid index 50, size is 0
请教我怎么做?
您收到 IndexOutOfBoundsException 的原因是因为您试图在您的第一个 if 语句中获取月份列表第 50 位的项目,而该列表没有成员。
以下代码应为您提供正确的月份列表和日期哈希。 (虽然我还没有测试过,所以可能会有一些错误。)
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);