2013-07-25 106 views
0

下面是我的代码,显示当前的一周的日期,现在我在我的代码中添加两个新的按钮 btnpreviousweek,btnnextweek我想显示下周和上周的按钮点击日期怎么做我做?我如何更改日期以显示下周的日期或前一周的日期?另外这个代码显示从周日日期周六,我只是想显示只在周一至周五,这是我的屏幕截图如何显示日历nextweek日历

enter image description here

public class MyCalendarActivity extends Activity implements OnClickListener { 
    private static final String tag = "MyCalendarActivity"; 

    private TextView currentMonth; 
    private ImageView prevMonth; 
    private ImageView nextMonth; 
    Button btnpreviousweek,btnnextweek; 

    private GridView calendarView; 
    private GridCellAdapter adapter; 
    private Calendar _calendar; 
    @SuppressLint("NewApi") 
    private int month, year; 
    private int actualYear, actualMonth, actualDay; 
    @SuppressWarnings("unused") 
    @SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" }) 
    private final DateFormat dateFormatter = new DateFormat(); 
    private static final String dateTemplate = "MMMM yyyy"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.my_calendar_view); 

     _calendar = Calendar.getInstance(Locale.getDefault()); 
     month = _calendar.get(Calendar.MONTH) + 1; 
     year = _calendar.get(Calendar.YEAR); 
     actualYear = year; 
     actualMonth = month; 
     actualDay = _calendar.get(Calendar.DAY_OF_MONTH); 

     Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: " 
       + year); 

     btnpreviousweek = (Button) this.findViewById(R.id.btnpreviousweek); 
     btnnextweek = (Button) this.findViewById(R.id.btnnextweek); 

     btnpreviousweek.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
      //////////////////////////  what i write here   ///////////////////////// 
      } 
     }); 

     btnnextweek.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       //////////////////////////  what i write here   ///////////////////////// 
      } 
     }); 

     prevMonth = (ImageView) this.findViewById(R.id.prevMonth); 
     prevMonth.setOnClickListener(this); 

     currentMonth = (TextView) this.findViewById(R.id.currentMonth); 
     currentMonth.setText(DateFormat.format(dateTemplate, 
       _calendar.getTime())); 

     nextMonth = (ImageView) this.findViewById(R.id.nextMonth); 
     nextMonth.setOnClickListener(this); 
     calendarView = (GridView) this.findViewById(R.id.calendar); 

     adapter = new GridCellAdapter(getApplicationContext(), 
       R.id.calendar_day_gridcell, month, year); 
     adapter.notifyDataSetChanged(); 
     calendarView.setAdapter(adapter); 
    } 

    /** 
    * 
    * @param month 
    * @param year 
    */ 
    private void setGridCellAdapterToDate(int month, int year) { 
     adapter = new GridCellAdapter(getApplicationContext(), 
       R.id.calendar_day_gridcell, month, year); 
     _calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_WEEK)); 
     currentMonth.setText(DateFormat.format(dateTemplate, 
       _calendar.getTime())); 
     adapter.notifyDataSetChanged(); 
     calendarView.setAdapter(adapter); 
    } 

    @Override 
    public void onClick(View v) { 
     if (v == prevMonth) { 
      if (month <= 1) { 
       month = 12; 
       year--; 
      } else { 
       month--; 
      } 
      Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: " 
        + month + " Year: " + year); 
      setGridCellAdapterToDate(month, year); 
     } 
     if (v == nextMonth) { 
      if (month > 11) { 
       month = 1; 
       year++; 
      } else { 
       month++; 
      } 
      Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: " 
        + month + " Year: " + year); 
      setGridCellAdapterToDate(month, year); 
     } 

    } 

    @Override 
    public void onDestroy() { 
     Log.d(tag, "Destroying View ..."); 
     super.onDestroy(); 
    } 

    // Inner Class 
    public class GridCellAdapter extends BaseAdapter implements OnClickListener { 
     private static final String tag = "GridCellAdapter"; 
     private final Context _context; 

     private final List<String> list; 
     private static final int DAY_OFFSET = 1; 
     private final String[] weekdays = new String[] { "Sun", "Mon", "Tue", 
       "Wed", "Thu", "Fri", "Sat" }; 
     private final String[] months = { "January", "February", "March", 
       "April", "May", "June", "July", "August", "September", 
       "October", "November", "December" }; 
     private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 
       31, 30, 31 }; 
     private int daysInMonth; 
     private int currentDayOfMonth; 
     private int currentWeekDay; 
     private Button gridcell; 
     private TextView num_events_per_day; 
     private final HashMap<String, Integer> eventsPerMonthMap; 
     private final SimpleDateFormat dateFormatter = new SimpleDateFormat(
       "dd-MMM-yyyy"); 

     // Days in Current Month 
     public GridCellAdapter(Context context, int textViewResourceId, 
       int month, int year) { 
      super(); 
      this._context = context; 
      this.list = new ArrayList<String>(); 
      Log.d(tag, "==> Passed in Date FOR Month: " + month + " " 
        + "Year: " + year); 
      Calendar calendar = Calendar.getInstance(); 
      setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH)); 

      setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK)); 

      Log.d(tag, "New Calendar:= " + calendar.getTime().toString()); 
      Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay()); 
      Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth()); 

      // Print Month 
      printMonth(month, year); 

      // Find Number of Events 
      eventsPerMonthMap = findNumberOfEventsPerMonth(year, month); 
     } 

     private String getMonthAsString(int i) { 
      return months[i]; 
     } 

     private String getWeekDayAsString(int i) { 
      return weekdays[i]; 
     } 

     private int getNumberOfDaysOfMonth(int i) { 
      return daysOfMonth[i]; 
     } 

     public String getItem(int position) { 
      return list.get(position); 
     } 

     @Override 
     public int getCount() { 
      return list.size(); 
     } 

     /** 
     * Prints Month 
     * 
     * @param mm 
     * @param yy 
     */ 
     private void printMonth(int mm, int yy) { 
      Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy); 
      int trailingSpaces = 0; 
      int daysInPrevMonth = 0; 

      int currentMonth = mm - 1; 
      String currentMonthName = getMonthAsString(currentMonth); 
      daysInMonth = getNumberOfDaysOfMonth(currentMonth); 

      Log.d(tag, "Current Month: " + " " + currentMonthName + " having " + 
       daysInMonth + " days."); 

      GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 
        getCurrentDayOfMonth()); 
      Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString()); 

      int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1; 
      trailingSpaces = currentWeekDay; 

      Log.d(tag, "Week Day:" + currentWeekDay + " is " + 
        getWeekDayAsString(currentWeekDay)); 
      Log.d(tag, "No. Trailing space to Add: " + trailingSpaces); 
      Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth); 

      if (cal.isLeapYear(cal.get(Calendar.YEAR))) 
       if (mm == 2) ++daysInMonth; 
       else if (mm == 3) 
        ++daysInPrevMonth; 

      // move at the begging of the week 
      int i = getCurrentDayOfMonth(); 
      int j = currentWeekDay; 
      while (i > 1 && j > 0) { 
       i--; 
       j--; 
      } 
      // print the week, starting at Sunday 
      for (j = 0; i <= daysInMonth && j < 7; i++, j++) { 
       Log.d(currentMonthName, String.valueOf(i) + " " + 
         getMonthAsString(currentMonth) + " " + yy); 
       // If it is the current date, printing in BLUE (Orange) 
       if (i == actualDay && mm == actualMonth && yy == actualYear) { 
        list.add(String.valueOf(i) + "-BLUE" + "-" + 
          getMonthAsString(currentMonth) + "-" + yy); 
       } else { 
        list.add(String.valueOf(i) + "-WHITE" + "-" + 
          getMonthAsString(currentMonth) + "-" + yy); 
       } 
      } 
     } 

     /** 
     * NOTE: YOU NEED TO IMPLEMENT THIS PART Given the YEAR, MONTH, retrieve 
     * ALL entries from a SQLite database for that month. Iterate over the 
     * List of All entries, and get the dateCreated, which is converted into 
     * day. 
     * 
     * @param year 
     * @param month 
     * @return 
     */ 
     private HashMap<String, Integer> findNumberOfEventsPerMonth(int year, 
       int month) { 
      HashMap<String, Integer> map = new HashMap<String, Integer>(); 

      return map; 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = convertView; 
      if (row == null) { 
       LayoutInflater inflater = (LayoutInflater) _context 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       row = inflater.inflate(R.layout.screen_gridcell, parent, 
         false); 
      } 

      // Get a reference to the Day gridcell 
      gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell); 
      gridcell.setOnClickListener(this); 

      // ACCOUNT FOR SPACING 

      Log.d(tag, "Current Day: " + getCurrentDayOfMonth()); 
      String[] day_color = list.get(position).split("-"); 
      String theday = day_color[0]; 
      String themonth = day_color[2]; 
      String theyear = day_color[3]; 
      if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) { 
       if (eventsPerMonthMap.containsKey(theday)) { 
        num_events_per_day = (TextView) row 
          .findViewById(R.id.num_events_per_day); 
        Integer numEvents = (Integer) eventsPerMonthMap.get(theday); 
        num_events_per_day.setText(numEvents.toString()); 
       } 
      } 

      // Set the Day GridCell 
      gridcell.setText(theday); 
      gridcell.setTag(theday + "-" + themonth + "-" + theyear); 
      Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-" 
        + theyear); 

      if (day_color[1].equals("GREY")) { 
       gridcell.setTextColor(getResources() 
         .getColor(R.color.lightgray)); 
      } 
      if (day_color[1].equals("WHITE")) { 
       gridcell.setTextColor(getResources().getColor(
         R.color.lightgray02)); 
      } 
      if (day_color[1].equals("BLUE")) { 
       gridcell.setText("(" + theday + ")"); 

       gridcell.setTextColor(getResources().getColor(R.color.orrange)); 
      } 
      return row; 
     } 

     @Override 
     public void onClick(View view) { 
      String date_month_year = (String) view.getTag(); 
      // selectedDayMonthYearButton.setText("Selected: " + date_month_year); 
      Log.e("Selected date", date_month_year); 
      try { 
       Date parsedDate = dateFormatter.parse(date_month_year); 
       Log.d(tag, "Parsed Date: " + parsedDate.toString()); 

      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 
     } 

     public int getCurrentDayOfMonth() { 
      return currentDayOfMonth; 
     } 

     private void setCurrentDayOfMonth(int currentDayOfMonth) { 
      this.currentDayOfMonth = currentDayOfMonth; 
     } 

     public void setCurrentWeekDay(int currentWeekDay) { 
      this.currentWeekDay = currentWeekDay; 
     } 

     public int getCurrentWeekDay() { 
      return currentWeekDay; 
     } 
    } 
} 
+0

你会得到任何解决方案? –

回答

1

此代码可以帮助你,

Calendar cal = GregorianCalendar.getInstance(); 
    cal.setTime(new Date()); 
    cal.add(Calendar.DAY_OF_YEAR, -k); 
    Date daysBeforeDate = cal.getTime(); 
    String start_dt1 = "" + daysBeforeDate; 

您可以用数字7替换k,这样您可以在七天前得到日期,就像您可以在k天后使用k而不是-k一样。

+0

如何在按钮单击时添加printMonth()函数是显示错误 未定义新方法printMonth(int,int)View.OnClickListener(){} – user2589245

+0

在onClick方法中调用函数,如public void onClick(View arg0){ printMonth(int,int); } –

+0

我使另一个功能名称printMonthh2(int int)并复制所有printMonthcode现在请告诉我如何更改下周的日子,当按钮单击????如何更改打印月份的代码以显示日期的nextweek? – user2589245