Как создать таблицу в Android с несколькими столбцами?

Я хочу создать таблицу в android с несколькими столбцами. Большинство примеров я видел с двумя столбцами. (Я новичок в Java и Android.) Мне нужно 3-4 столбца, и я должен иметь возможность динамически добавлять строки в таблицу. Может ли кто-нибудь предоставить мне образец кода. (Я использую eclipse в win 7)

Ответы

Ответ 1

Я предполагаю, что вы говорите о представлении TableLayout, а не в таблице в базе данных

Если это так, вот пример XML таблицы с тремя столбцами и тремя строками.

Элемент TableRow > создает строку в таблице, и каждый вид внутри элемента создает "столбец". Я использовал TextViews, но они могут быть ImageViews, EditText и т.д.

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id = "@+id/RHE"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="0"
         android:padding="5dp">

     <TableRow android:layout_height="wrap_content">
         <TextView
             android:id="@+id/runLabel"
             android:text="R"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/hitLabel"
             android:text="H"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/errorLabel"
             android:text="E"
             android:layout_height="wrap_content"
             />
     </TableRow>

     <TableRow android:layout_height="wrap_content">
         <TextView
             android:id="@+id/visitorRuns"
             android:text="0"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/visitorHits"
             android:text="0"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/visitorErrors"
             android:text="0"
             android:layout_height="wrap_content"
             />
     </TableRow>

     <TableRow android:layout_height="wrap_content">
         <TextView
             android:id="@+id/homeRuns"
             android:text="0"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/homeHits"
             android:text="0"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/homeErrors"
             android:text="0"
             android:layout_height="wrap_content"
             />
     </TableRow>
</TableLayout>

Чтобы динамически изменить их в коде, у вас будет что-то вроде этого:

// reference the table layout
TableLayout tbl = (TableLayout)findViewById(R.id.RHE);
// delcare a new row
TableRow newRow = new TableRow(this);
// add views to the row
newRow.addView(new TextView(this)); // you would actually want to set properties on this before adding it
// add the row to the table layout
tbl.addView(newRow);