2011-04-04 108 views
2

我想在運行期間在我的android應用程序中動態創建一組複選框。除了按鈕,應用程序什麼都不顯示。我忘了什麼?提前致謝!動態複選框創建

public class DataNotificationSurvey extends Activity { 
    private Date timeStamp; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.datanotifylayout); 
     final Button notifySubmitButton = (Button) findViewById(R.id.notifySubmitButton); 
     TableLayout t1 = (TableLayout)findViewById(R.id.notificationTableLayout); 

     for(int i = 0; i < 5; i++) { 
      TableRow tr = new TableRow(this); 
      CheckBox chk = new CheckBox(this); 
      chk.setText(Integer.toString(i)); 
      tr.addView(chk); 
      t1.addView(tr); 
     } 

     notifySubmitButton.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         finish(); 
        } 
       }); 
}  

//My xml layout, will be changing this later to the one posted below to see if it works. 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:id="@+id/notificationLinearLayout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TableLayout 
    android:id="@+id/notificationTableLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <Button android:text="Static Button"/> 
     </TableRow> 
    </TableLayout> 
    <Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/notifySubmitButton" 
    android:text="Submit"></Button> 
</LinearLayout> 
+0

請發佈您的layout.xml以及代碼看起來沒問題,所以問題可能在那裏。 – harpo 2011-04-04 03:53:29

+0

@harpo:添加了xml,稍後我會更改以反映發佈爲答案的xml。如果它有效,我會讓你知道。但是,如果您在xml或其他任何地方發現問題,請通知我。謝謝 – prolink007 2011-04-04 15:54:44

回答

1

這工作對我很好。

public class DynamicTableRowWithCheckBox extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final Button notifySubmitButton = (Button) findViewById(R.id.myButton); 
    TableLayout table = (TableLayout) findViewById(R.id.myTable); 

    for (int i = 0; i < 3; i++) 
    { 
     TableRow row = new TableRow(this); 
     CheckBox chk = new CheckBox(this); 
     chk.setText(Integer.toString(i)); 
     row.addView(chk); 
     table.addView(row); 
    } 

    notifySubmitButton.setOnClickListener(
     new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
      finish(); 
      } 
     }); 
    } 
}
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 
    <Button 
     android:id="@+id/myButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Close" /> 
    <TableLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:id="@+id/myTable" /> 
</LinearLayout>
+0

明天就試試這個,讓你知道結果。我的xml與你的不同,很快就會改變,看它是否有效。謝謝。 – prolink007 2011-04-04 05:15:37

+0

發表我當前的XML,當我有機會我會嘗試你的XML,看看會發生什麼。但是,如果您發現我到目前爲止有任何問題,請告訴我。我可能沒有機會在今晚晚些時候嘗試你的xml。謝謝 – prolink007 2011-04-04 15:55:38

+0

這是什麼顯示給你?因爲它只是爲我顯示兩個按鈕。沒有行,也沒有字符串表示for循環中的「i」值。 – prolink007 2011-04-04 23:03:40

0

你的XML更改爲:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:id="@+id/notificationLinearLayout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TableLayout 
     android:id="@+id/notificationTableLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TableRow> 
      <Button 
       android:text="Static Button" /> 
     </TableRow> 
    </TableLayout> 
    <Button 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/notifySubmitButton" 
     android:text="Submit"></Button> 
</LinearLayout>

這將刪除你的TableLayout爲wrap_content的layout_height,並刪除高度和你的TableRow的寬度設置,因爲,"The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT."

+0

這仍然不顯示像我打算的行。它只顯示靜態按鈕和提交按鈕。這就是它所顯示的。我需要它也顯示行。 – prolink007 2011-04-04 21:31:20

+0

這個XML文件需要定位:要設置的垂直值。 – prolink007 2011-04-04 23:37:10

+0

啊,是的。 LinearLayout的默認方向是水平的。 – 2011-04-05 00:35:55