Afa
Afa

Reputation: 81

dynamically added table is not displayed in android

there is something going wrong , such that firstly i created separately xml file for table row ,it did not work, then i created table row and text view programatically, again it is not displayed when i run , i can not get if what is wrong with my code

    table = (TableLayout) findViewById(R.id.goodsTable);     
    for (int i = 0; i < attrName.length; i++) {

                            TableRow tr = new TableRow(getApplicationContext());
                            tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

                            TextView atName = new TextView(getApplicationContext());

                            atName.setText(attrName[i]);
                            tr.addView(atName);
                            table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));


                        }
                        table.requestLayout();

Upvotes: 1

Views: 58

Answers (1)

Abhishek Patel
Abhishek Patel

Reputation: 4328

Try This it may be help to you

for (int i = 0; i < attrName.length; i++) {

         TableRow tr = new TableRow(this);
         tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,   TableRow.LayoutParams.WRAP_CONTENT));

         TextView atName = new TextView(this);
         atName.setText(attrName[i]);
         atName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
         tr.addView(atName);
         table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}

You must apply LayoutParam for TextView and for create new Object for TableRow or TextView you should use Activity Context not ApplicationContext

Upvotes: 2

Related Questions