简体   繁体   中英

TextView arrays in TableLayout

I am wanting to do 4 columns with dynamically changing rows of data. I would do this with LinearLayout, but I cannot find a good way to get a 4x10 set of textviews. This .java is trowing a fatal error which is listed at the bottom:

public class DebtList extends Activity {

Integer trigger = 5;
Double totalDebt = 0.00;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.debtlist);

    //Bundle extras = getIntent().getExtras();

    String[] debtNames = new String[10];
    Double[] debtAmount = new Double[10];
    Double[] debtRate = new Double[10];
    Double[] debtTerm = new Double[10];

    Integer stopper = trigger+1;

    TableLayout tl=(TableLayout)findViewById(R.id.debtlist);
    TableRow tr1 = new TableRow(this);
    TextView textview = new TextView(this);

    for(int i=0; i<stopper; i++)
    {

        tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tr1.setId(i);

        textview.setText("" + i);
        tr1.addView(textview);
        tl.addView(tr1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        tr1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT));
        tr1.setId(i);

    }

    trigger++;
}
}

I have a (basically) blank .XML to work with:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/debtlist"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</TableLayout>

my LogCat:

10-13 04:12:07.412: E/AndroidRuntime(799): FATAL EXCEPTION: main
10-13 04:12:07.412: E/AndroidRuntime(799): java.lang.ClassCastException: android.app.ActionBar$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:587)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.TableLayout.measureVertical(TableLayout.java:468)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.TableLayout.onMeasure(TableLayout.java:431)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
10-13 04:12:07.412: E/AndroidRuntime(799):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1890)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.View.measure(View.java:10828)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.ViewRoot.performTraversals(ViewRoot.java:909)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.os.Looper.loop(Looper.java:132)
10-13 04:12:07.412: E/AndroidRuntime(799):  at android.app.ActivityThread.main(ActivityThread.java:4025)
10-13 04:12:07.412: E/AndroidRuntime(799):  at java.lang.reflect.Method.invokeNative(Native Method)
10-13 04:12:07.412: E/AndroidRuntime(799):  at java.lang.reflect.Method.invoke(Method.java:491)
10-13 04:12:07.412: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-13 04:12:07.412: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-13 04:12:07.412: E/AndroidRuntime(799):  at dalvik.system.NativeStart.main(Native Method)

If anyone has any more efficient ways to get a 4X1-10 set of TextViews to be displayed based on the amount of data input by the user, please let me know.

Also I'm using API level 12

as in log :

ClassCastException: android.app.ActionBar$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

means you are importing ActionBar.LayoutParams for setting LayoutParams to TableLayout and Buttons so use LinearLayout.LayoutParams as:

for(int i=0; i<stopper; i++)
    {

     tr1.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT,
                                                     LayoutParams.WRAP_CONTENT));
        //.....
     tr1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                                                    LayoutParams.WRAP_CONTENT)));
     tr1.setId(i);

    }

You are getting class cast exception because you are using Table Layout and inflating context of ActionBar Linear Layout.

java.lang.ClassCastException: android.app.ActionBar$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

is there a way 4 columns per row for x number of rows ?

Use a custom layout for this and use it .

 for(int i=0; i<stopper; i++)
    {

       // Inflate your row "template" and fill out the fields.
    TableRow row = (TableRow)LayoutInflater.from(context).inflate(R.layout.attrib_row,  null);
    ((TextView)row.findViewById(R.id.attrib_name)).setText(b.NAME);
    ((TextView)row.findViewById(R.id.attrib_value)).setText(b.VALUE);
    table.addView(row);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM