Dynamically filling a Table Layout with Table Rows

Karim M. El Tel picture Karim M. El Tel · Sep 25, 2012 · Viewed 7.8k times · Source

I have a table layout inside a scroll view , I want to fill them dynamically with 5 N Table Rows. Each Table Row contains an ImageView and an EditText. The app crashed Here is the complete stack:

    09-25 10:03:07.496: E/AndroidRuntime(1695): FATAL EXCEPTION: main
09-25 10:03:07.496: E/AndroidRuntime(1695): java.lang.ClassCastException: android.widget.TableLayout$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.TableRow.mapIndexAndColumns(TableRow.java:164)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.TableRow.getVirtualChildCount(TableRow.java:149)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.TableRow.getColumnsWidths(TableRow.java:289)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.TableLayout.findLargestCells(TableLayout.java:504)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.TableLayout.measureVertical(TableLayout.java:469)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.TableLayout.onMeasure(TableLayout.java:435)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1196)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.ScrollView.onMeasure(ScrollView.java:318)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.View.measure(View.java:15172)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.Choreographer.doFrame(Choreographer.java:525)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.os.Handler.handleCallback(Handler.java:615)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.os.Looper.loop(Looper.java:137)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at java.lang.reflect.Method.invokeNative(Native Method)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at java.lang.reflect.Method.invoke(Method.java:511)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-25 10:03:07.496: E/AndroidRuntime(1695):     at dalvik.system.NativeStart.main(Native Method)

CODE:

private TableLayout Table;//in XML

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

                //here I define the TR params   
        Table=(TableLayout)findViewById(R.id.Table);
        TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams 
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
            //fill the table with Table rows dynamicly
    for(int i=0;i<N;i++){


        TableRow TR= new TableRow(this);
        TR.setLayoutParams(tableRowParams);
        Table.addView(TR);
        //Table Row contains EditText and ImageView
                    EditText editText1 =new EditText(this);
        ImageView imgview=new ImageView(this);
        TR.addView(editText1);
        TR.addView(imgview);

        editText1.setText("foo");
        editText1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));

        int id = getResources().getIdentifier("com.example.university2:drawable/news1", null, null);


        imgview.setImageResource(id);
        imgview.setAdjustViewBounds(true);
        imgview.setMaxHeight(50);
        imgview.setMaxWidth(50);

        imgview.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));


    }

}

Here is the XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >

    <TableLayout
        android:id="@+id/Table"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableLayout>

</ScrollView>

I would appreciate your help. Thank you.

Answer

ra. picture ra. · Sep 25, 2012

Looks that issue is caused by TR.setLayoutParams(tableRowParams);
(java.lang.ClassCastException: android.widget.TableLayout$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams)
becouse tableRowParams (declaration: TableLayout.LayoutParams tableRowParams) is not TableRow.LayoutParams
Try to replace

TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);

with (updated)

TableRow.LayoutParams tableRowParams=new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f);

and edit

TR.addView(editText1, new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f));
TR.addView(imgview, new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f));

remove

imgview.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));

and

editText1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));

Issue was caused by incorrect creation of LayoutParams for particular control added to particular owner, e.g.: if You are adding control to LinearLayout - You should use LinearLayout.LayoutParams for that control, TableLayout.LayoutParams - in case You are adding control to TableLayout, ... etc...