2014-03-03 37 views
0

我想在運行時向TableLayout添加行,但數據根本沒有顯示出來。動態添加行到TableLayout沒有顯示

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1" > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbarSize="10dp" 
     android:scrollbars="horizontal|vertical" > 

     <TableLayout 
      android:id="@+id/server_dataTable" 
      android:layout_width="match_parent" 
      android:layout_height="300dp" 
      android:background="@color/color_data_table_header_background" 
      android:stretchColumns="*" > 
     </TableLayout> 
    </ScrollView> 

</LinearLayout> 

相關的代碼:

private TableLayout addRowToTable(TableLayout table, String[] data) { 
    Context context = this.getApplicationContext(); 
    TableRow row = new TableRow(context); 

    TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(); 
    // Wrap-up the content of the row 
    rowParams.height = TableLayout.LayoutParams.WRAP_CONTENT; 
    rowParams.width = TableLayout.LayoutParams.WRAP_CONTENT; 

    for (int i = 0; i < data.length; i++) { 
     TableRow.LayoutParams columnParams = new TableRow.LayoutParams(); 
     // wrap-up content of the row 
     columnParams.height = TableRow.LayoutParams.WRAP_CONTENT; 
     columnParams.width = TableRow.LayoutParams.WRAP_CONTENT; 
     // set gravity to center of the column 
     columnParams.gravity = Gravity.CENTER; 
     TextView column = new TextView(context); 
     column.setText(data[i]); 
     row.addView(column, columnParams); 
    } 

    table.addView(row, rowParams); 

    return table; 
} 

我打電話從我活動的onCreate()addRowToTable

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_server); 
    // Show the Up button in the action bar. 
    setupActionBar(); 

    TableLayout dataTableHeader = (TableLayout) findViewById(R.id.server_dataTable); 
    dataTableHeader = addRowToTable(dataTableHeader, headerLabels); 
} 

我已經在這了現在大約40分鐘,已經閱讀了幾乎所有關於StackOverflow的相關問題,但都沒有找到任何有用的。任何幫助/指針都會很棒!

編輯:添加了活動的onCreate()。

+0

你的'addRowToTable'運行在UI線程上嗎? –

+0

是啊!我應該線程嗎? –

+0

發佈您的所有代碼 –

回答

1

傳遞Activity背景到您的addRowToTable方法類似

dataTableHeader = addRowToTable(youractivity.this,dataTableHeader, headerLabels); 

和訪問到addRowToTable像:

private TableLayout addRowToTable(Activity a,TableLayout table, String[] data) { 
TableRow row = new TableRow(a); 
........ 
........ 
table.addView(row); 

return table; 
} 

試試這個辦法,給我反饋

更新:嘗試這個方式:

private TableLayout addRowToTable(Activity a,TableLayout table, String[] data) { 
TableRow row = new TableRow(a); 

row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
for (int i = 0; i < data.length; i++) { 

    TableRow.LayoutParams columnParams = new TableRow.LayoutParams(); 
    // wrap-up content of the row 
    columnParams.height = TableRow.LayoutParams.WRAP_CONTENT; 
    columnParams.width = TableRow.LayoutParams.WRAP_CONTENT; 
    // set gravity to center of the column 
    columnParams.gravity = Gravity.CENTER; 
    TextView column = new TextView(a); 
    column.setText(data[i]); 
    row.addView(column,columnParams); 
} 

table.addView(row); 

return table; 
} 
+0

仍然不起作用。活動中沒有任何東西出現。 :/ –

+0

@AbhishekBhardwaj現在,告訴我一件事情** headerLabels **檢查這不是空和/或你在** headerLabels **添加數據的地方? –

+0

不爲空。我嘗試在'column.setText()'之後輸出'column.getText()。toString()'到LogCat並輸出標籤名稱。 –