2011-01-21 83 views
0

我正在使用tablelayout创建一个日历作为我的屏幕显示。但是,当我需要用户选择新的月份或年份更新显示时,它只会将前面存储的数据添加到表格中。显示正在添加TABLELAYOUT

这里是我的代码:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.main); 

    table = new TableLayout(this); 

    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(   
      ViewGroup.LayoutParams.FILL_PARENT,   
      ViewGroup.LayoutParams.FILL_PARENT); 
    table.setLayoutParams(lp); 
    table.setStretchAllColumns(true); 
    rowLp2 = new TableLayout.LayoutParams(   
      ViewGroup.LayoutParams.FILL_PARENT,   
      ViewGroup.LayoutParams.FILL_PARENT,   
      1.0f); 
    cellLp2 = new TableRow.LayoutParams(   
      ViewGroup.LayoutParams.FILL_PARENT,   
      ViewGroup.LayoutParams.FILL_PARENT,   
      1.0f); 
    // cellLp3 = new TableRow.LayoutParams(arg0, arg1) 
setYYMMDD(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_WEEK)); 
    leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1; 
    //calling methods 
    header(); 
    weeks(); 
    //setLeadGap(2011, 2); 
    days(dom[mm]); 


    setContentView(table); 
} 

private void days(int noDays) 
{ 
    TableRow row = new TableRow(this); 
    int d=1; 
    nRow = (leadGap > 3) ? nRow+1 : nRow; 
    //textMonth.setText(Integer.toString(nRow)); 
    for (int r = 0; r <nRow ; ++r) 
    {  
     row = new TableRow(this); 
     if (r==0) 
      { int l = 0; 
       for (int f = 1; f<7; ++f) 
       { 
        if(l <leadGap) 
        { 
          TextView v = new TextView(this); 
          v.setText(""); 

          row.addView(v, cellLp2); 

          l++; 
        } 
        if (l == leadGap) 
        { 
         Button v = new Button(this); 
         v.setText(Integer.toString(d)); 
         row.addView(v, cellLp2); 
         d++; 
        } 
        row.setBackgroundColor(Color.rgb(55, 254, 55)); 

       } 
      } 
      else 
      { 

       for (int f = 0; f < 7; ++f)  
       { 

         if (d <= noDays) 
         { 
          //Button btn = new Button(this); 
          //btn.setText(Integer.toString(d));   
          //row.addView(btn, cellLp2); 
          Button v = new Button(this); 
          v.setText(Integer.toString(d)); 
          row.addView(v, cellLp2); 

          row.setBackgroundColor(Color.rgb(55, 254, 55)); 
          d++; 
         } 
         else 
         { 
          TextView btn = new TextView(this); 
          btn.setText("");   
          row.addView(btn, cellLp2); 

         } 


       } 
      } 
     table.addView(row, rowLp2); 
    } 

} 

private void header(){ 
    headertbl= new TableLayout(this); 
    btnMonthPos = new Button(this); 
    btnMonthPos.setOnClickListener(new View.OnClickListener() 
    {    
     public void onClick(View v) 
     { 
      if (iMonth > 10) 
      { 
       iMonth = 0; 
      } 
      else 
      { 
       iMonth += 1; 
      } 
      setLeadGap(iYear, iMonth); 
      days(dom[iMonth]); 



      textMonth.setText(months[iMonth]); 
      //recompute(); 
     } 


    }); 
textMonth = new TextView(this); 
btnMonthNeg = new Button(this); 
    btnMonthNeg.setOnClickListener(new View.OnClickListener() 
    {    
     public void onClick(View v) 
     { 
      if (iMonth < 1) 
      { 
       iMonth = 11; 
      } 
      else 
      { 
       iMonth -= 1; 
      } 
      textMonth.setText(months[iMonth]);   
     }   
    }); 
btnYearPos = new Button(this); 
    btnYearPos.setOnClickListener(new View.OnClickListener() 
    {    
     public void onClick(View v) 
     { 
      iYear += 1; 
      textYear.setText(Integer.toString(iYear)); 
      leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1; 
      //sunBtn.setText(Integer.toString(leadGap)); 
     }   
    }); 
textYear = new TextView(this); 
btnYearNeg = new Button(this); 
    btnYearNeg.setOnClickListener(new View.OnClickListener() 
     {    
      public void onClick(View v) 
      { 
       iYear -= 1; 
       textYear.setText(Integer.toString(iYear));   
      }   
     }); 

//setting values during loading 
btnMonthPos.setText("++"); 
textMonth.setText(months[mm]); 
btnMonthNeg.setText("--"); 
btnYearPos.setText("++"); 
textYear.setText(Integer.toString(calendar.get(Calendar.YEAR))); 
btnYearNeg.setText("--"); 

TableRow headerRow = new TableRow(this); 
headerRow.addView(btnMonthPos, cellLp2); 
headerRow.addView(textMonth, cellLp2); 
headerRow.addView(btnMonthNeg, cellLp2); 
headerRow.addView(btnYearPos, cellLp2); 
headerRow.addView(textYear, cellLp2); 
headerRow.addView(btnYearNeg, cellLp2); 

headertbl.addView(headerRow, rowLp2); 

table.addView(headertbl); 

} 

private void weeks() 
{ 
    String[] weekName = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 
    tblWeek = new TableLayout(this); 
    TableRow rowWeek = new TableRow(this); 

    for (int i = 0; i<1; i++) 
    { 
     rowWeek = new TableRow(this); 
     for (int j=0; j<weekName.length;j++) 
     { 
      TextView day = new TextView(this); 
      day.setText(" "+weekName[j]); 
      day.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
      rowWeek.addView(day, cellLp2); 
     } 
     rowWeek.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
     tblWeek.addView(rowWeek, rowLp2); 
    } 

    table.addView(tblWeek); 
} 
} 

回答

0

考虑星期添加新行()天()等函数表之前删除旧tablerows(Tablelayout)

+0

您好,感谢您的答复。我现在的问题是我不知道从我以前的表/视图中删除行。我尝试使用removeView(row)循环,但它只会给我错误。请帮忙。谢谢 – 2011-01-24 01:51:49