2012-07-22 98 views
0

我想在表格中顯示來自我的數據庫的一些評論,從評論數據庫中獲取數據是可以的,但在我的活動中沒有任何內容顯示我的頁面的標題,我只是可以「找不到錯誤:S動態表格佈局

下面是Java的一部分:

public class PIComments extends Activity{ 

     public static final String EXTRA_IDPI="EXTRA_IDPI"; 
     TableLayout messagesTable; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.picomments); 
      messagesTable = (TableLayout)findViewById(R.id.commentTable); 

      long PIID = getIntent().getLongExtra(EXTRA_IDPI, 0); 

      if(PIID>0){ 

       ArrayList<CommentMsg> comments = CommentMsg.getPIComments(PIID); 

       if(comments != null){ 

        for(int i=0; i<comments.size(); i++) { 

         // Create a TableRow and give it an ID 
         TableRow tr = new TableRow(this); 
         tr.setId(i); 
         tr.setLayoutParams(new LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.WRAP_CONTENT)); 

         // Create a TextView to house the name of the province 
         TextView login = new TextView(this); 
         login.setId(i); 
         login.setText(comments.get(i).getMsg()); 
         //login.setText(comments.get(i).getAuteur()); 
         login.setTextColor(Color.BLACK); 
         login.setLayoutParams(new LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.WRAP_CONTENT)); 
         tr.addView(login); 

         // Add the TableRow to the TableLayout 
         messagesTable.addView(tr, new TableLayout.LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.WRAP_CONTENT)); 

        } 
       } 
       else{ 
        Dialog d = new Dialog(PIComments.this); 
        d.setTitle("No comments for the moment ... "); 
        d.show(); 
       } 
      } 

     } 

} 

且有XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffccd0" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dp" 
     android:background="#ffb0b6" 
     android:text="Comments" 
     android:textColor="#b3000d" 
     android:textSize="26dp" 
     android:textStyle="bold" /> 

    <LinearLayout 
     android:id="@+id/ll_country" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <ScrollView 
      android:id="@+id/ScrollView11" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:fillViewport="true" > 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="5dp" > 

       <TableLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/commentTable" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:stretchColumns="0" > 
       </TableLayout> 

      </LinearLayout> 
     </ScrollView> 
    </LinearLayout> 

</LinearLayout> 
+0

如果我的回答可以幫助你,那麼請不要忘記接受我的回答,並投票給我也會非常感激。謝謝:) – SALMAN 2012-07-22 15:06:13

回答

1

明白了,每次的LayoutParams應android.widget.TableRow的。 LayoutPara MS只是一個提供給tl.addView(...)

/* Find Tablelayout defined in main.xml */ 
TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines); 
/* Create a new row to be added. */ 
TableRow tr = new TableRow(this); 
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
/* Create a Button to be the row-content. */ 
Button b = new Button(this); 
b.setText("Dynamic Button"); 
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
/* Add Button to row. */ 
tr.addView(b); 
/* Add row to TableLayout. */ 
//tr.setBackgroundResource(R.drawable.sf_gradient_03); 
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); 

OR

我有同樣的問題,然後我刪除

textView.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, 
LayoutParams.WRAP_CONTENT)); 
從每個TextView的

,顯示的東西。

OR

設置你的TextView顏色

textView.setTextColor(Color.WHITE); 

可能存在一種可能性,即你的背景是黑色的文本顏色:)

沿着並且仍然不低於幫助請參考這個非常好的教程,可能有可能你錯過了一些東西。 Dynamic TableLayout in Android

我希望這會幫助你。

謝謝:)

+0

對不起,但我看不出你的代碼和我的任何區別除了你使用的是按鈕,我使用textView,並且你的tl在我的活動中被稱爲messagesTable:S – Karly 2012-07-22 19:51:12

+0

:)你是否專注於這行「得到它,每個LayoutParams應該是android.widget.TableRow.LayoutParams,除了提供給tl.addView(...)的一個。」 – SALMAN 2012-07-22 20:00:42

+0

對於LayoutParams,導入:)可能有所不同。請重新檢查一下,你必須使用android.widget.TableRow.LayoutParams,除了提供給tl.addView(...) – SALMAN 2012-07-22 20:04:01