您好我正在使用Caldroid库在我的习惯跟踪器应用程序中自定义日历视图,并且有两个哈希映射successMap
和failureMap
表示习惯成功和失败的日子。
我正在为成功的日子设置绿色圆形背景drawable,为失败的日子设置红色圆形背景,但只显示其中一个,后面会在代码中写入。
以下是设置日历的方法:
private void setCalendar(){
Map<Date, Drawable> successMap = new HashMap<>();
Map<Date, Drawable> failureMap = new HashMap<>();
//For success days
for(int i=0; i<successDays.size(); i++){
successMap.put(successDays.get(i), getResources().getDrawable(R.drawable.green_circular));
}
//For failure days
for(int i=0; i<failureDays.size(); i++){
failureMap.put(failureDays.get(i), getResources().getDrawable(R.drawable.red_circular));
}
caldroidFragment.setBackgroundDrawableForDates(successMap);
caldroidFragment.setBackgroundDrawableForDates(failureMap);
caldroidFragment.refreshView();
}
仅此处显示的日期会在稍后显示,例如此处仅显示失败的日期。我已检查过这些arraylists并映射了值,它们很好。所以我该如何解决这个问题?
答案 0 :(得分:2)
检查Caldroid库后,我发现每次调用setBackgroundDrawableForDates()时它都清除了以前的List
public void setBackgroundDrawableForDates(
Map<Date, Drawable> backgroundForDateMap) {
if (backgroundForDateMap == null || backgroundForDateMap.size() == 0) {
return;
}
backgroundForDateTimeMap.clear();
for (Date date : backgroundForDateMap.keySet()) {
Drawable drawable = backgroundForDateMap.get(date);
DateTime dateTime = CalendarHelper.convertDateToDateTime(date);
backgroundForDateTimeMap.put(dateTime, drawable);
}
}
所以对你的问题的解决方法是附加两个列表togthers并且只调用一次setBackgroundDrawableForDates()以避免重置 喜欢那个
private void setCalendar(){
Map<Date, Drawable> successMap = new HashMap<>();
Map<Date, Drawable> failureMap = new HashMap<>();
//For success days
for(int i=0; i<successDays.size(); i++){
successMap.put(successDays.get(i), getResources().getDrawable(R.drawable.green_circular));
}
//For failure days
for(int i=0; i<failureDays.size(); i++){
failureMap.put(failureDays.get(i), getResources().getDrawable(R.drawable.red_circular));
}
successMap.putAll(failureMap);
caldroidFragment.setBackgroundDrawableForDates(successMap);
caldroidFragment.refreshView();
}