2016-12-27 3 views
-1

Ich habe den folgenden Code:Hinzufügen Tage zum Kalender nicht auf Android arbeiten

public void createWeeks(JSONArray TargetDays){ 
    int helper, targetDayPos, curDayPos; 
    JSONArray dateContainer = new JSONArray(); 
    JSONArray dateData = new JSONArray(); 
    Calendar c = Calendar.getInstance(); 
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); 
    Utilities dayConverter = new Utilities(getApplicationContext()); 

    for(int i=0 ; i<TargetDays.length() ; i++){ 
     try { 
      targetDayPos = dayConverter.getDayPosition(TargetDays.getString(i)); 
      curDayPos = dayConverter.getCalenderDayPosition(dayOfWeek); 
      if (curDayPos > targetDayPos) { 
       helper = curDayPos - targetDayPos; 
       c.add(Calendar.DATE, -helper); 
      } 
      else if(targetDayPos > curDayPos){ 
       helper = targetDayPos - curDayPos; 
       c.add(Calendar.DATE, helper); 
      } 

      dateData.put(c.get(Calendar.YEAR)); 
      dateData.put(c.get(Calendar.MONTH)+1); 
      dateData.put(c.get(Calendar.DAY_OF_MONTH)+1); 
      Toast.makeText(getApplicationContext(),curDayPos+"\n"+targetDayPos,Toast.LENGTH_LONG).show(); 
      dateContainer.put(dateData); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    try { 
     weekDayList.put("days",TargetDays); 
     weekDayList.put("dates", dateContainer); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

Ich bin eine Reihe von Tagen in einer Woche und dessen Zeitpunkt hier zu schaffen. aber das Hinzufügen von Tagen funktioniert nicht und am Ende bekomme ich nur das Datum von heute.

+0

Könnten Sie machen einen [Minimal, vollständig und prüfbare Beispiel] (http://stackoverflow.com/help/ mcve), bitte? Ich bin mir sicher, dass es (uns und Ihnen) sehr helfen würde. –

+0

Ich brauche Hilfe über "add" -Methode des Kalenders. es macht nichts. nichts passiert. nachdem ich Tage damit hinzugefügt habe, und dann bekomme ich Zeit, es ist immer noch das gleiche Datum vor dem Hinzufügen. @Ole V.V. –

+0

Ich habe ziemlich großes Vertrauen in 'Calendar.add()'. Ich vermute, dein Problem ist woanders. Hast du einen Debugger probiert? –

Antwort

0

Ich fand das Problem und als @Ole V.V. sagte in dem Kommentar, es ging nicht um Kalender. das Problem war über seinen Algorithmus. Es ging darum, wie ich das JSON-Array erstellte.

ich es änderte sich dies und behandelt sie in einer anderen Art und Weise in meinem Programm:

public void createWeeks(JSONArray TargetDays){ 
    int helper, targetDayPos, curDayPos; 
    JSONArray dateContainer = new JSONArray(); 
    Date currentDate = new Date(); 
    Calendar c = Calendar.getInstance(); 
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); 
    Utilities dayConverter = new Utilities(getApplicationContext()); 

    try { 
     for(int i=0 ; i<TargetDays.length() ; i++){ 

      c.setTime(currentDate); 
      targetDayPos = dayConverter.getDayPosition(TargetDays.getString(i)); 
      curDayPos = dayConverter.getCalenderDayPosition(dayOfWeek); 
      if (curDayPos > targetDayPos) { 
       helper = curDayPos - targetDayPos; 
       c.add(Calendar.DATE, -helper); 
      } 
      else if(targetDayPos > curDayPos) { 
       helper = targetDayPos - curDayPos; 
       c.add(Calendar.DATE, helper); 
      } 
      dateContainer.put(dateFormat.format(c)); 

     } 
     weekDayList.put("days",TargetDays); 
     weekDayList.put("dates", dateContainer); 


    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
Verwandte Themen