Problem in displaying data in the Tablelayout
I have used a TableLayout to display data. Data is in json format so for that I have created dynamic TableRow and TextView with filled data and putted into the TableLayout. In test application I have taken a fix string array to filled the table in place of json. It doesn't matter.
The problem is that all code runs well without error. Even I have debug each line of code. I didn't find any error. Event then My data is not showing in a table. TableLayout remains blank as in starting. My code is:
@Override
public void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabular_layout);
final TableLayout tl = (TableLayout)findViewById(R.id.tblLyt);
for(int i = 0; i<names.length; i++)
{
TableRow tr = new TableRow(this);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
createView(tr, tv1, Integer.toString(i+1));
createView(tr, tv2, names[i]);
tl.addView(tr);
}
}
catch(Exception ex)
{
开发者_运维百科 MessageBox(ex.getMessage());
}
}
public void createView(TableRow tr, TextView t, String viewdata) throws Exception{
try
{
t.setText(viewdata);
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
t.setTextColor(Color.DKGRAY);
t.setBackgroundColor(Color.WHITE);
t.setPadding(20, 0, 0, 0);
tr.setPadding(0, 1, 0, 1);
tr.addView(t); // add TextView to row.
}
catch(Exception ex)
{
throw ex;
}
}
This is my tabular_layout.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<TableLayout android:layout_width="fill_parent" android:id="@+id/tblLyt" android:layout_height="wrap_content" android:layout_weight="0.69">
</TableLayout>
</LinearLayout>
I am still new in Android. I might done silly mistake. But I tried a lot on this and also searched for that. So help me !!
Thanks.
Problem is in the following line.block it and run your program
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
/** * You need to return tablerow and hold in mainlayout. use below code /
public TableRow createView(TableRow tr, TextView t, String viewdata) throws Exception{ try { t.setText(viewdata); t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); t.setTextColor(Color.DKGRAY); t.setBackgroundColor(Color.WHITE); t.setPadding(20, 0, 0, 0); tr.setPadding(0, 1, 0, 1); tr.addView(t); // add TextView to row. } catch(Exception ex) { throw ex; }
retuen tr; }
// call like dynamica layout tl.addView(createView(tr, tv1, Integer.toString(i+1));
精彩评论