user2424370
user2424370

Reputation:

Dynamically Populate Data for Android Table Layout

I am trying to load the data retrieve from SQLite database into Android tableLayout. Here is my code:

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;
    }
}

When I print out the mCur.getString(j), it did returning me the string. However, it does not appear in the tableLayout or showing me any error message. I wonder why is it so.

Edited portion

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);
                        }

                    }

Edited Portion

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;
    }

}

Upvotes: 6

Views: 7582

Answers (1)

user
user

Reputation: 87074

You're not using the proper LayoutParams for the views that you programmatically add to the TableLayout. The created TableRows will be added to the TableLayout so their LayoutParams should be of type TableLayout.LayoutParams:

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

The same goes for the row TextViews, which should use TableRow.LayoutParams:

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

Upvotes: 5

Related Questions