动态填充Android表格布局的数据

时间:2014-07-30 08:18:16

标签: java android sql sqlite android-tablelayout

我正在尝试将从SQLite数据库中检索的数据加载到Android tableLayout中。这是我的代码:

TableLayout table_layout;
private SQLiteDatabase mDb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabhost);

    new Handler().post(new Runnable() { 
        @Override 
        public void run() { 
            DatabaseAdapter dbAdapter = new DatabaseAdapter(Exercise.this); 
            dbAdapter.createDatabase();
            Exercise.this.mDb =  new DataBaseHelper(Exercise.this).getReadableDatabase();  
            table_layout = (TableLayout) findViewById(R.id.TableLayout);
            BuildTable();
        }
    });
}

private void BuildTable() {
    try {
        String sql = "SELECT exerciseType FROM exercise";
        Cursor mCur = mDb.rawQuery(sql, null);
        if (mCur.getCount() != 0) {
            if (mCur.moveToFirst()) {
                do {
                    int rows = mCur.getCount();
                    int cols = mCur.getColumnCount();
                    // 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.setGravity(Gravity.CENTER);
                            tv.setTextSize(18);
                            tv.setPadding(0, 5, 0, 5);

                            tv.setText(mCur.getString(j));
                            row.addView(tv);                            

                        }
                        table_layout.addView(row);
                    }
                } while (mCur.moveToNext());
            }
        }
    } catch (SQLException mSQLException) {
        throw mSQLException;
    }
}

当我打印出mCur.getString(j)时,它确实将字符串返回给我。但是,它不会出现在tableLayout中或显示任何错误消息。我想知道为什么会这样。

已编辑的部分

for (int j = 0; j < cols + 1; j++) {
                        TextView tv = new TextView(this);
                        tv.setLayoutParams(new TableRow.LayoutParams(
                                TableLayout.LayoutParams.MATCH_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        tv.setGravity(Gravity.CENTER);
                        tv.setTextSize(18);
                        tv.setPadding(0, 5, 0, 5);
                        tv.setText(mCur.getString(j));

                        row.addView(tv);
                        if(j == cols){
                            CheckBox cb = new CheckBox(this);
                            cb.setLayoutParams(new TableRow.LayoutParams(
                                    TableLayout.LayoutParams.MATCH_PARENT,
                                    LayoutParams.WRAP_CONTENT));
                            cb.setGravity(Gravity.CENTER);
                            cb.setPadding(0, 5, 0, 5);
                            row.addView(cb);
                        }

                    }

已编辑的部分

private void BuildTable() {
    try {
        String sql = "SELECT * FROM exercise";
        Cursor mCur = mDb.rawQuery(sql, null);
        if (mCur.getCount() != 0) {
            if (mCur.moveToFirst()) {

                TableRow row = new TableRow(this);
                row.setLayoutParams(new LayoutParams(
                        TableLayout.LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));

                TextView tv1 = new TextView(this);
                tv1.setLayoutParams(new TableRow.LayoutParams(
                        TableLayout.LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
                tv1.setGravity(Gravity.LEFT);
                tv1.setTextSize(10);
                tv1.setText("ID");
                row.addView(tv1);

                TextView tv2 = new TextView(this);
                tv2.setLayoutParams(new TableRow.LayoutParams(
                        TableLayout.LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
                tv2.setGravity(Gravity.LEFT);
                tv2.setTextSize(10);
                tv2.setText("Exercise Type");
                row.addView(tv2);

                TextView tv3 = new TextView(this);
                tv3.setLayoutParams(new TableRow.LayoutParams(
                        TableLayout.LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
                tv3.setGravity(Gravity.LEFT);
                tv3.setTextSize(10);
                tv3.setText("Amount");
                row.addView(tv3);

                TextView tv4 = new TextView(this);
                tv4.setLayoutParams(new TableRow.LayoutParams(
                        TableLayout.LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
                tv4.setGravity(Gravity.LEFT);
                tv4.setTextSize(10);
                tv4.setText("");
                row.addView(tv4);

                do {
                    int cols = mCur.getColumnCount();

                    for (int j = 0; j < cols + 1; j++) {
                        if (j == cols) {
                            CheckBox cb = new CheckBox(this);
                            cb.setLayoutParams(new TableRow.LayoutParams(
                                    TableLayout.LayoutParams.MATCH_PARENT,
                                    LayoutParams.WRAP_CONTENT));
                            cb.setGravity(Gravity.LEFT);
                            row.addView(cb);
                            final int k = j;
                            cb.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    exerciseIDList.add(Integer.toString(k));
                                }
                            });
                            break;
                        }

                        TextView tv = new TextView(this);
                        tv.setLayoutParams(new TableRow.LayoutParams(
                                TableLayout.LayoutParams.MATCH_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        tv.setGravity(Gravity.LEFT);
                        tv.setTextSize(10);
                        tv.setText(mCur.getString(j));

                        row.addView(tv);

                    }
                    table_layout.addView(row);
                } while (mCur.moveToNext());
            }
        }
    } catch (SQLException mSQLException) {
        throw mSQLException;
    }

}

1 个答案:

答案 0 :(得分:5)

对于以编程方式添加到TableLayout的视图,您没有使用正确的LayoutParams。创建的TableRows将添加到TableLayout中,因此它们的LayoutParams应为TableLayout.LayoutParams类型:

row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

行TextViews也是如此,它应该使用TableRow.LayoutParams:

tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));