TableLayout dinamico
En Cajon de-sastre se puede escoger el registro de una tabla para editar sus campos. Necesitaba crear un formulario dinámico en función de los campos de esta tabla. Para esto he escogido el TableLayout que es equivalente al tag ‘table’ de HTML.

El layout tab.xml utilizado:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:stretchColumns="1"
android:id="@+id/miTab">
<Button
android:id="@+id/alta"
android:layout_width="wrap_content"
android:text="@string/botIns"
android:layout_height="wrap_content">
</Button>
<Button
android:id="@+id/modif"
android:layout_width="wrap_content"
android:text="@string/botUpd"
android:layout_height="wrap_content">
</Button>
<Button
android:id="@+id/borra"
android:layout_width="wrap_content"
android:text="@string/botDel"
android:layout_height="wrap_content">
</Button>
</TableLayout>
</ScrollView>
Rutinas implicadas:
/* Obtener campos de la tabla menos campo _id */
public String[][] getCmps(String tab) {
String s = "PRAGMA table_info(" + tab + ")";
Cursor c = execCons(s);
int num = c.getCount() - 1;
String cmps[][] = new String[num][2];
int i = 0;
c.moveToFirst();
c.moveToNext(); // salta campo _id
while (c.isAfterLast() == false) {
cmps[i][0] = c.getString(1);
cmps[i++][1] = c.getString(2).substring(0, 1);
// Log.w("getCmp1", i + " " + c.getString(1));
c.moveToNext();
}
c.close();
return cmps;
}
/* se completa miTab dinamicamente con los campos del fichero */
protected void añadirCmps() {
TableLayout tl = (TableLayout)findViewById(R.id.miTab);
for(int i = 0; i < cmps.length ; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tvs[i] = new TextView(this);
tvs[i].setText(cmps[i][0]);
tr.addView(tvs[i]);
ets[i] = new EditText(this);
ets[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
if (cmps[i][1].equals("i")) ets[i].setInputType(InputType.TYPE_CLASS_NUMBER);
if (cmps[i][1].equals("r")) ets[i].setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
if (cmps[i][1].equals("d")) ets[i].setInputType(InputType.TYPE_DATETIME_VARIATION_NORMAL);
if (cmps[i][1].equals("t")) ets[i].setInputType(InputType.TYPE_CLASS_TEXT);
if (mRowId != null) ets[i].setText(c.getString(i + 1));
// Log.w("añadirCmps", cmps[i][0] + " : " +cmps[i][1]);
tr.addView(ets[i]);
tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
}
La función getCmps obtiene la definición de los campos de la tabla. Esta definición guÃa la creación dinámica del formulario: una linea (row) pr campo con el nombre del campo y su valor editable (editText) indicando si es numérico, alfanumérico o fecha.