0

I have code for programmatically adding rows to table

TableLayout tl = (TableLayout) findViewById(R.id.tblLog);
                    List<String> s=new LinkedList<String>();
                    s.add("test1");
                    s.add("test2");
                    s.add("test3");
                    // Go through each item in the array
                    int id=0;
                    for (String temp:s)
                    {
                        id++;
                        // Create a TablView v = new View(this);eRow and give it an ID
                        TableRow tr = new TableRow(currentActivity);  
                        TableLayout.LayoutParams tableRowParams=
                          new TableLayout.LayoutParams
                          (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

                        int leftMargin=10;
                        int topMargin=2;
                        int rightMargin=10;
                        int bottomMargin=2;

                        tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

                        tr.setLayoutParams(tableRowParams);
                        // Create a TextView to house the name of the province
                        TextView labelTV = new TextView(currentActivity);
                        labelTV.setId(200+id);
                        labelTV.setText(temp);
                        labelTV.setTextColor(Color.RED);
                        labelTV.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        tr.addView(labelTV);
                        tl.addView(tr, new TableLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                    }   

and I have xml

    <TableLayout
        android:id="@+id/tblLog"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
>
    </TableLayout>

and I pass through function without exception, I have debugged, but nothing shows on screen. Can somebody tell me what I have done wrong ?

1 Answer 1

1

Perhaps, you need to have other views you want to display on the row inside the TableLayout. Try adding your TextView inside the TableLayout.

Or in your existing code

tl.addView(tr);

Since once you set the parameters for the TableRow

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.