Android Rokers Android Rokers - 3 months ago 31
Android Question

how to bind data from ArrayList to TableLayout in Android?

i have one problem to create my application in Android.
Here In my Application I will Bind the ArrayList Data into TableLayout.
when i will run the application that time only last column data are bind. i have require all column are bind. Please help me...

I have provide my code:

View Task.xml,

<TableLayout
android:id="@+id/score_table1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow></TableRow>
</TableLayout>


view task.java,

TableLayout table = (TableLayout) findViewById(R.id.score_table1);

for(int i=0;i<object.size();i++)
{


TableRow row=new TableRow(ViewTask.this);

@SuppressWarnings("unchecked")
ArrayList<String> data1 = (ArrayList<String>) object.get(i);

TextView taskdate = new TextView(ViewTask.this);
taskdate.setTextSize(10);
taskdate.setText(data1.get(0).toString());
row.addView(taskdate);

TextView title = new TextView(ViewTask.this);
taskdate.setText(data1.get(1).toString());
row.addView(title);
taskdate.setTextSize(10);


TextView taskhour = new TextView(ViewTask.this);
taskdate.setText(data1.get(2).toString());
taskhour.setTextSize(10);
row.addView(taskhour);


TextView description = new TextView(ViewTask.this);
taskdate.setText(data1.get(3).toString());
row.addView(description);
description.setTextSize(10);

table.addView(row);

}

Answer

You're creating multiple textView in the for loop but setting text only to the first textTiew.

 TextView taskdate = new  TextView(ViewTask.this);
                taskdate.setTextSize(10);
                taskdate.setText(data1.get(0).toString());
                row.addView(taskdate);

                TextView title = new  TextView(ViewTask.this);
                taskdate.setText(data1.get(1).toString());
                row.addView(title);
                taskdate.setTextSize(10);


                TextView taskhour = new  TextView(ViewTask.this);
                taskdate.setText(data1.get(2).toString());
                taskhour.setTextSize(10);
                row.addView(taskhour);


                TextView description = new  TextView(ViewTask.this);
                taskdate.setText(data1.get(3).toString());
                row.addView(description);
                description.setTextSize(10);

Observe that with the taskdate variable, you are setting text multiple times.

Comments