如何在Android中的运行时动态检索TableLayout中的所有列?

时间:2014-07-11 09:23:23

标签: java android android-tablelayout

我希望在运行时从SQLite动态获取表格布局中的14列。我试过但得到了ArithmeticException的异常:除以零。我不知道mutateColumnsWidth.I表格布局中的实际问题在哪里浮出网但没有找到合适的解决方案。有人可以帮帮我。如何解决这个问题。感谢高级。

这是我的活动代码。

SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
        Cursor c1 =sqlDatabase.rawQuery(performanceQuery , null);

          int rows = c1.getCount();
          Log.e("rows "," = " + rows);

          int cols = c1.getColumnCount();
          Log.e("cols "," = " + cols);

        if (c1 != null && c1.getCount() != 0) 
        {  
            Log.e("11111","11111");
            if (c1.moveToFirst())
            {   
                Log.e("222222","222222");
                // outer for loop
                  for (int i = 0; i < rows; i++) 
                  {
                     TableRow row = new TableRow(this);
                      row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                                 LayoutParams.WRAP_CONTENT));


                    // inner for loop
                       for (int j = 0; j < cols; j++)
                       {
                           TextView tv = new TextView(this);

                            tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                            tv.setBackgroundResource(R.drawable.cell_shape);
                            tv.setGravity(Gravity.CENTER);
                            tv.setTextSize(18);
                          // tv.setTextColor(010101);
                           tv.setPadding(5, 5, 5, 5);

                            tv.setText(c1.getString(j));

                            String strText = tv.getText().toString().trim();
                            System.out.println(" strText " + strText);

                            row.addView(tv);


                       }
                       Log.e("3333333","3333333");
                       c1.moveToNext();

                       Log.e("4444444","4444444");
                       table_layout.addView(row);


                  }
            }
            sqlDatabase.close();
            c1.close();
            Log.e("55555555","55555555");
        }

Here is log error information

07-11 14:49:59.665: E/AndroidRuntime(328): FATAL EXCEPTION: main
07-11 14:49:59.665: E/AndroidRuntime(328): java.lang.ArithmeticException: divide by zero
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:579)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.widget.TableLayout.shrinkAndStretchColumns(TableLayout.java:568)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.widget.TableLayout.measureVertical(TableLayout.java:466)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.widget.TableLayout.onMeasure(TableLayout.java:431)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.view.View.measure(View.java:8313)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.view.View.measure(View.java:8313)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.view.View.measure(View.java:8313)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.view.View.measure(View.java:8313)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.os.Looper.loop(Looper.java:123)
07-11 14:49:59.665: E/AndroidRuntime(328):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-11 14:49:59.665: E/AndroidRuntime(328):  at java.lang.reflect.Method.invokeNative(Native Method)
07-11 14:49:59.665: E/AndroidRuntime(328):  at java.lang.reflect.Method.invoke(Method.java:507)
07-11 14:49:59.665: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-11 14:49:59.665: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-11 14:49:59.665: E/AndroidRuntime(328):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案