Мой код:
public class Test extends Activity {
private TableLayout tableLayout = null;
private Button add = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
tableLayout = (TableLayout)findViewById(R.id.tableLayout);
add = (Button)findViewById(R.id.agregar);
add.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
TableRow fila = new TableRow(Test.this);
Button eliminar = new Button(Test.this);
eliminar.setText("delete");
eliminar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
fila.addView(eliminar,new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tableLayout.addView(fila,new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
});
}
Я хочу добавить новую строку каждый раз, когда я нажимаю add Button
.Мой xml следующий:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:text="Delete" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/agregar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tableLayout"
android:text="Add" />
</RelativeLayout>
Каждый раз, когда я нажимаю Button
, он ничего не делает.Может ли кто-нибудь помочь мне с этим?