2013-09-04 4 views
1

내 코드는 달력을 Button이 .IT는 Button가 또 매주 basis.Below의 날짜가 내 코드 및 로그 캣입니다 업데이트하려면 클릭 할 때 캘린더를 업데이트하지 않습니다 클릭 한 번만으로 업데이트버튼 클릭시 메소드를 업데이트하는 방법은 무엇입니까?

public class myCalendar extends Activity { 

public final static String[] monthcalender = { "January", "February", 
     "March", "April", "May", "June", "July", "August", "September", 
     "October", "November", "December" }; 

public final static int daysinmonths[] = { 31, 28, 31, 30, 31, 30, 31, 31, 
     30, 31, 30, 31 }; 

int mon, yr; 
int j=0; 
Calendar todaycldr; 

// myCalendar moncldr = new myCalendar(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_calendar); 

    Button button1 = (Button) findViewById(R.id.button1); 



//  if (mon == 2) { 
    //  displayMonth(Integer.parseInt(args[0]) - 1, 
//  Integer.parseInt(args[1])); 
//  } else { 
    todaycldr = Calendar.getInstance(); 
displayMonth(todaycldr.get(Calendar.MONTH), todaycldr.get(Calendar.YEAR)); 
    // } 


    button1.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      j=7; 
      displayMonth(todaycldr.get(Calendar.MONTH), 
    todaycldr.get(Calendar.YEAR)); 

     } 
    }); 

} 

private void displayMonth(int month, int year) { 

    // The number of days to leave blank at 
    // the start of this month. 

    int blankdays = 0; 
    System.out.println(" " + monthcalender[month] + " " + year); 

    if (month < 0 || month > 11) { 
     throw new IllegalArgumentException("Month " + month 
       + " is not valid and must lie in between 0 and 
    11"); 
    } 

    GregorianCalendar cldr = new GregorianCalendar(year, month, 1); 
    System.out 
      .println("Sunday Monday Tuesday Wednesday Thursday Friday 
Saturday"); 

    // Compute how much to leave before before the first day of the month. 
    // getDay() returns 0 for Sunday. 

    blankdays = cldr.get(Calendar.DAY_OF_WEEK) - 1; 
    int daysInMonth = daysinmonths[month]; 

    if (cldr.isLeapYear(cldr.get(Calendar.YEAR)) && month == 1) { 

     ++daysInMonth; 
    } 

    // Blank out the labels before 1st day of the month 
    for (int i = 0; i < blankdays; i++) { 
     System.out.print(" "); 
    } 

    for (int i = 1; i <= daysInMonth; i++) { 

     // This "if" statement is simpler than messing with NumberFormat 
     if (i <= 9) { 
      System.out.print(" "); 
     } 
     System.out.print(i+j); 

     if ((blankdays + i) % 7 == 0) { // Wrap if EOL 
      System.out.println(); 
     } else { 
      System.out.print(" "); 
     } 
     if (i % 7 ==0) 
     { 
      break; 
     } 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_calendar, menu); 
    return true; 
} 

    } 

로그 캣

09-04 01:00:37.727: I/System.out(591): September 2013 
    : Sunday Monday Tuesday Wednesday Thursday Friday Saturday 
    1 2 3 4 5 6 7 
     September 2013 
System.out(591): Sunday Monday Tuesday Wednesday Thursday Friday Saturday 
    System.out(591): 8 9 10 11 12 13 14 
I/System.out(591): September 2013 
: I/System.out(591): Sunday Monday Tuesday Wednesday Thursday Friday Saturday 
I/System.out(591): 8 9 10 11 12 13 14 
I/System.out(591): September 2013 
    I/System.out(591): Sunday Monday Tuesday Wednesday Thursday Friday Saturday 
09-04 01:01:20.017: I/System.out(591): 8 9 10 11 12 13 14 

어떤 제안은 매우 appreciated.Thanks 될 것입니다.

답변

0

생성자를 생성하고이 코드 이동 :

public myCalendar() { 
     // TODO Auto-generated constructor stub 
     todaycldr = Calendar.getInstance(); 
      year = todaycldr.get(Calendar.YEAR); 
    monthOfYear = todaycldr.get(Calendar.MONTH); 


    } 

와 한 OnCreate에서 호출 displayMonth :

displayMonth(monthOfYear, year); 
관련 문제