Wednesday, 9 July 2014

How to make dynamic table using android xml?



Guys i have a XML file containing TableLayout as follows:



<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp" >

<TableLayout
android:id="@+id/resulttable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3" >

</TableLayout>
</ScrollView>


Here is another layout named row.xml as follows



<TableRow xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/tr"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_discription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<TextView
android:id="@+id/tv_left_vehicle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<TextView
android:id="@+id/tv_right_vehicle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>

</TableRow>


In the above table i want to show array list of string, for this i have done following code:



TableLayout table = (TableLayout) findViewById(R.id.resulttable);
for(int i=0;i<DArray.size();i++)
{

LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
TableRow row = (TableRow) inflater.inflate(R.layout.row, table, false);

String discription = DArray.get(i);
String leftVehicle = LArray.get(i);
String rightVehicle = RArray.get(i);

TextView tvD = (TextView)row.findViewById(R.id.tv_discription);
tvD.setText(discription);
TextView tvlPrice = (TextView)row.findViewById(R.id.tv_left_vehicle);
tvlPrice.setText(leftVehicle);
TextView tvrPrice = (TextView)row.findViewById(R.id.tv_right_vehicle);
tvrPrice.setText(rightVehicle);

row.addView(tvD);
row.addView(tvlPrice);
row.addView(tvrPrice);
table.addView(row);
}


but this code is arising exception. Here is LogCat error for my code.



E/AndroidRuntime(1897): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


I am beginner for this situation please help me find out the problem.


for more detail i want to make a table like this:


enter image description here


No comments:

Post a Comment