简体   繁体   中英

Adding rows to a TableLayout dynamically in Android

I am working on an application that needs to add rows to a table with 2 columns dynamically . Below is the code i am using .... i am getting a "ForceClose" error ... plss help ....

<?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
<Button android:id="@+id/b1" />
</TableRow>
    </TableLayout>

this is my java code

            TableLayout tb=(TableLayout)findViewById(R.id.table);
    TableRow tr=new TableRow(getBaseContext());
    Button b1=(Button)findViewById(R.id.b1);
    LayoutParams lp=new         LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

    tr.setLayoutParams(lp);
    b1.setLayoutParams(lp);
    b1.setText("I NEED HELP ! SOS ");

    tr.addView(b1);
    tb.addView(tr,lp);

tb.addView(tr,lp); has a bug.. Lp is Layout Param which is already set to TableRow so just use tb.addView(tr); instead of tb.addView(tr,lp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM