Android动态表行创建问题

时间:2011-09-21 11:58:03

标签: android android-layout

我是这个网站的新手。请指出我是否有任何错误 我有xml.That包含TableLayout.Table行动态插入。

这是我的xml:

    <?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:background="@drawable/wallpaper">

<ScrollView android:layout_width="fill_parent"  
            android:layout_height="wrap_content" 
            android:id="@+id/sView1" 
            android:scrollbars="vertical">

    <LinearLayout android:id="@+id/lLayout2" 
                  android:layout_width="fill_parent" 
                  android:layout_height="396dp">

        <HorizontalScrollView android:id="@+id/hScrollView1" 
                              android:layout_width="fill_parent" 
                              android:layout_height="wrap_content" >

            <LinearLayout android:id="@+id/lLayout3" 
                          android:layout_width="fill_parent" 
                          android:layout_height="fill_parent" 
                          android:orientation="horizontal">

                <TableLayout  android:stretchColumns="*" 
                              android:layout_width="fill_parent" 
                              android:layout_margin="5pt" 
                              android:layout_marginTop="5dp"  
                              android:id="@+id/lineTable" 
                              android:layout_height="fill_parent">

                </TableLayout>
            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</ScrollView>



 <!-- Footer -->
    <LinearLayout android:id="@+id/header"
        android:background="#000000"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">

        <Button android:layout_width="wrap_content" 
                android:text="Cancel" 
                android:background="@drawable/btn_yellow"
                android:layout_height="wrap_content" 
                android:layout_marginLeft="5dp"
                android:layout_marginRight="190dp"
                android:id="@+id/button2" 
                android:textColor="#FFFFFF"
                android:onClick="onCancelAction">
       </Button>

        <Button android:layout_width="wrap_content" 
                android:text="Complete" 
                android:background="@drawable/btn_yellow"
                android:layout_height="wrap_content" 
                android:textColor="#FFFFFF"
                android:id="@+id/button2" 
                android:onClick="onCompleteAction">
       </Button>
    </LinearLayout>

这是动态表行创建:

    private  void loadTableLayout(){

    TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            1.0f);
    TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            1.0f);

    //Table Header
    TableRow th = new TableRow(this);

    TextView thCode = new TextView(this);
    thCode.setTextSize(1, 12);
    thCode.setTextColor(Color.BLACK);
    thCode.setText("Criteria");
    thCode.setPadding(10, 0, 12, 0);
    th.addView(thCode,cellLp); 

    TextView thDes = new TextView(this);
    thDes.setTextSize(1, 12);
    thDes.setTextColor(Color.BLACK);
    thDes.setText("Name");
    thDes.setPadding(10, 0, 16, 0);
    th.addView(thDes,cellLp); 

    TextView thQty = new TextView(this);
    thQty.setTextSize(1, 12);
    thQty.setText("DisQty");
    thQty.setTextColor(Color.BLACK);
    thQty.setPadding(10, 0, 10, 0);
    th.addView(thQty,cellLp); 

    TextView thValue = new TextView(this);
    thValue.setTextSize(12);
    thValue.setText("DisValue");
    thValue.setPadding(10, 0, 10, 0);
    thValue.setTextColor(Color.BLACK);
    th.addView(thValue,cellLp); 

    TextView thDisVal = new TextView(this);
    thDisVal.setTextSize(12);
    thDisVal.setText("DisProductCode");
    thDisVal.setPadding(10, 0, 8, 0);
    thDisVal.setTextColor(Color.BLACK);
    th.addView(thDisVal,cellLp); 
    th.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_shape));
    tl.addView(th,rowLp);

    if(lineDisList.size() > 0){
        //dynamic Table Contents
        for (int i = 0; i <lineDisList.size(); i++) {

            TableRow tr = new TableRow(this);   
            tr.setTag(i);

            //Set PDA Top panel data, when the list is load
            if(i == 0){

            }
            System.out.println(" --- " + lineDisList.get(i).getProductCode());
            System.out.println(" --- " + lineDisList.get(i).getCriteriaName());
            System.out.println(" --- " + lineDisList.get(i).getDiscountQty());
            System.out.println(" --- " + lineDisList.get(i).getDiscountValue());
            System.out.println(" --- " + lineDisList.get(i).getDiscountProductCode().length);


            TextView critiria = new TextView(this);
            critiria.setTextSize(12);
            critiria.setText(lineDisList.get(i).getProductCode());
            critiria.setPadding(10, 0, 10, 0);
            tr.addView(critiria,cellLp); 



            TextView description = new TextView(this);
            description.setTextSize(12);
            description.setText(lineDisList.get(i).getCriteriaName());
            description.setPadding(10, 0, 10, 0);
            description.setTextColor(Color.BLACK);
            tr.addView(description,cellLp);

            TextView disValue = new TextView(this);
            disValue.setTextSize(12);
            disValue.setPadding(10, 0, 10, 0);
            disValue.setTextColor(Color.BLACK);
            disValue.setText(Double.toString(lineDisList.get(i).getDiscountValue()));
            tr.addView(disValue,cellLp);


            TextView disQuantity = new TextView(this);
            disQuantity.setTextSize(12);
            disQuantity.setText("0.00");
            disQuantity.setPadding(10, 0, 10, 0);
            disQuantity.setTextColor(Color.BLACK);
            tr.addView(disQuantity,cellLp);

            tr.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_shape));
            tl.addView(tr,rowLp);

        }
    }

}

我遇到了这种错误:

    09-21 17:18:01.959: ERROR/AndroidRuntime(389): FATAL EXCEPTION: main
09-21 17:18:01.959: ERROR/AndroidRuntime(389): java.lang.StackOverflowError
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6880)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6883)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6986)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.widget.HorizontalScrollView.draw(HorizontalScrollView.java:1409)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6986)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.widget.ScrollView.draw(ScrollView.java:1409)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6883)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6883)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6883)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6883)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6883)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6883)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6883)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.View.draw(View.java:6883)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewRoot.draw(ViewRoot.java:1522)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 17:18:01.959: ERROR/AndroidRuntime(389):     at android.os.L

请请大家,如果您知道任何建议或想法,请告诉我我的代码有什么问题?

由于