2016-12-10 140 views
0

嗨,我使用Caldroid庫來定製我的習慣跟蹤器應用中的日曆視圖,並且有兩個散列圖successMapfailureMap表示一個習慣的成功和失敗的日子。 我爲成功的日子設置了可繪製的綠色圓形背景,爲失敗的日子繪製了紅色的圓形背景,但僅顯示了其中一個稍後以代碼形式寫入的背景。 下面是設置日曆,方法:Caldroid的可繪製背景

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(); 
} 

在這種只有日期顯示將在稍後的書面例如這裏不僅沒有幾天就會shown.I已經檢查了這些的ArrayList和地圖價值,他們都很好。那麼我該如何解決這個問題呢?

回答

2

檢查Caldroid庫後,我發現,每次調用setBackgroundDrawableForDates(時間庫)清楚前面的列表

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); 
    } 
} 

所以soluation你的問題是追加兩個列表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(); 
    } 
+0

謝謝@Ahmed馬哈茂德是解決問題:) –

+0

我嘗試一次選擇只有一個日期,並設置選定日期的背景顏色,但它在所有選擇的日期上應用顏色。如果選擇1,然後更改其背景顏色,但是如果我選擇2,則顏色只應用於2並從1中移除 – Erum