2014-12-06 48 views
0

我想創建一個非常簡單的調度器插入數據到表中。我沒有問題獲得這些信息,但是,我似乎無法插入到有意義的表格中。由有意義表,我的意思是開始作爲這樣,隨後添加插入低於其尊重標題的表:添加到表格Android TableLayout

天:

第2天:

第3天:

第4天:

我從用戶處獲得課程信息,並希望根據課程發生時間表進行分類。我正在把這個問題組織到一張桌子上,這很困難。

有關如何有效排序的建議?

回答

0

使用TableLayout並使用XML創建佈局模板。例如:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" > 

     <TextView 
      android:id="@+id/textView1" 
      android:text="DAY 1:" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" > 

     <TextView 
      android:id="@+id/textView1" 
      android:text="DAY 2:" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" > 

     <TextView 
      android:id="@+id/textView1" 
      android:text="DAY 3:" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" > 

     <TextView 
      android:id="@+id/textView1" 
      android:text="DAY 4:" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

    </TableRow> 

</TableLayout> 

然後檢查的課程信息的列表,併爲每個課程加入編程信息關於它作爲一個TableRow到先前創建的佈局。爲此,請使用TableLayout#addView(View child, int index)。例如添加TableRow含有TextView作爲最後一排TableLayout

TableLayout tl = (TableLayout) findViewById(R.id.my_table_layout); 
TableRow tr = new TableRow(context); 
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
TextView tv = new TextView(context); 
tv.setText("Some text"); 
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
tr.addView(tv); 
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); 
+0

沒關係,這是有道理的。謝謝。我如何更新表格? – Matthew 2014-12-08 23:04:05

+0

使用'TableLayout#addView'方法創建'TableRow'並將它們添加到'TableLayout'中。查看更新的答案。 – 2014-12-08 23:54:00

相關問題