2014-03-03 2 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()가 추가되었습니다.

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

return table; 
} 

이 방법을 시도하고 피드백을 나에게

업데이트를 제공합니다 :이 밖으로 시도 같은 addRowToTable

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

및 액세스처럼 addRowToTable 방법에

+0

'addRowToTable'이 UI 스레드에서 실행되고 있습니까? –

+0

예! 나는 그것을 실어야합니까? –

+0

모든 코드를 게시하십시오. –

답변

1

통과 Activity 컨텍스트 way :

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 ** 이것이 null이 아니거나 ** headerLabels **에 데이터를 추가하는 위치를 확인 하시겠습니까? –

+0

null이 아닙니다. 'column.getText(). toString()'을'column.setText()'바로 다음에 LogCat에 출력하고 레이블 이름을 출력했습니다. –

관련 문제