2017-04-07 30 views
1

我正在處理我的項目,我想在應用程序啓動時顯示日曆顯示,哪個日期包含事件,例如如果日期包含事件,則日按鈕包含*符號和日期如果日期不包含任何事件,那麼它只顯示一天。 我寫了下面的代碼,但是當我點擊那個按鈕時,它只顯示*符號,那麼我怎麼能管理這個代碼,該代碼在應用程序啓動時只包含事件的日期顯示*符號,或者該頁面將被加載。Codename一個日曆UpdateButtonDayDate()問題

以下是我的代碼: -

public class Customised extends Calendar{ 
      ArrayList<String[]> data = new ArrayList<>(); 
      int i,j,columns; 



      @Override 
      protected void updateButtonDayDate(Button dayButton,int currentMonth, int day) { 
     dayButton.setText(""+day); 



      dayButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent evt) {  
//Check which date having how many number of events=============================================================== 

      try{ 
       ShowEvent.removeAll(); 
      cur = db.executeQuery("SELECT Event, Description from CalendarData WHERE Date = ? ", dateLabel.getText()); 
      columns = cur.getColumnCount(); 
      if(columns > 0) { 
      boolean next = cur.next(); 
      if(next) {    
      String[] columnNames = new String[columns]; 
      for(int iter = 0 ; iter < columns ; iter++) { 
      columnNames[iter] = cur.getColumnName(iter); 
      } 
      while(next) { 
      Row currentRow = cur.getRow(); 
      String[] currentRowArray = new String[columns]; 
      for(int iter = 0 ; iter < columns ; iter++) { 
      currentRowArray[iter] = currentRow.getString(iter); 
      } 
      data.add(currentRowArray); 
      next = cur.next(); 
      } 
      Object[][] arr = new Object[data.size()][]; 
      data.toArray(arr); 
      } 
      } 
      }catch(IOException e){ 
      e.printStackTrace(); 
      } 
      for(i = 0 ; i< data.size(); i++){ 
      Log.p(data.get(i)[0]); 
      } 

      Label a = new Label(dateLabel.getText()); 
      Label b = new Label("   "+i); 
      Container container1 = TableLayout.encloseIn(2, a,b); 
      container1.setUIID("container1"); 

      ShowEvent.add(container1); 


       for(i = 0 ; i< data.size(); i++){ 
       for(j = 0; j<columns; j++){ 
      Log.p(data.get(i)[j]); 
      SpanLabel spanData = new SpanLabel(data.get(i)[j]); 
      spanData.setUIID("SpanLabel"); 
      ShowEvent.add(spanData);  
      } 
      Label space = new Label("======================="); 
      ShowEvent.add(space); 
      Log.p("###################"); 
      } 
      data.clear(); 

      if(i>0){ 
       if(Dialog.show("Choose action", "What you want to do?", "Add Events","View Events")){ 
        calendar.show(); 
       } 
        else{ 
        ShowEvent.show(); 
       } 
      }else{ 
       Dialog.show("Add event","There is no event to display, Please add events first","OK",""); 

      } 
//============================================================================================================ 
      } 
      }); 
      } 

      @Override 
      protected void initComponent(){ 
       ArrayList<String[]> data1 = new ArrayList<>(); 
       int k; 
       Log.p("initComponent"); 

      try{ 
       cur = db.executeQuery("select Date from CalendarData"); 
       columns = cur.getColumnCount(); 
      if(columns > 0) { 
      boolean next = cur.next(); 
      if(next) {    

      String[] columnNames = new String[columns]; 
      for(int iter = 0 ; iter < columns ; iter++) { 
      columnNames[iter] = cur.getColumnName(iter); 
      } 
      while(next) { 
      Row currentRow = cur.getRow(); 
      String[] currentRowArray = new String[columns]; 
      for(int iter = 0 ; iter < columns ; iter++) { 
      currentRowArray[iter] = currentRow.getString(iter); 
      } 
      data1.add(currentRowArray); 
      next = cur.next(); 
      } 
      Object[][] arr = new Object[data1.size()][]; 
      data1.toArray(arr); 
      } 
      } 
      }catch(IOException e){ 
      e.printStackTrace(); 
      } 
      for(k = 0 ; k< data1.size(); k++){ 
      Log.p(data1.get(k)[0]); 

      } 
      if(k>0){ 
       //cal.setUIID("CalendarSelectedDay"); 
      } 

      } 

      /* 
      @Override 
      protected boolean isInitialized(){ 
       boolean result = false; 
       Log.p("isInitialised"); 
       return result; 
      }*/ 

      public Customised(){ 

      } 





      @Override 
      protected Button createDay() { 
      Button day = new Button(); 
      day.setAlignment(CENTER); 
      day.setUIID("CalendarDay1"); 
      day.setEndsWith3Points(false); 
      day.setTickerEnabled(false); 
      return day; 
      } 



      } 

和預期的結果將是: -

enter image description here

回答

1

那是因爲你放在actionPerformed方法是裏面的代碼僅在按下按鈕/釋放按鈕時觸發。 移動你的代碼到updateButtonDayDate範圍

+0

我已經這樣做了。但它在那裏也不起作用。 –

+0

當我在updateButtonDayDate作用域中複製該代碼時,它將得到'我'值0.所以它不會進入,如果循環。 –

+0

任何人都可以知道如何做到這一點? –