首页 > 解决方案 > 在 Tablelayout 上设置 SQLite 的内容复选框并显示选中的复选框的值

问题描述

在 Android Studio 中,我在 TableLayout TableRows 上有复选框,它将显示来自 SQLite 数据库的值 id。每当我单击“显示”按钮时,我都想获取所有选中复选框的值。

我无法显示要显示的值。而 for- 和 while 循环使每个 id 显示三次。

SQLiteDatabase db = dataHelper.getReadableDatabase();
String selectQuery = "SELECT * FROM table2 where column2 =" + 1;
Cursor cursor = db.rawQuery(selectQuery, null);
   if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
    int id = cursor.getInt(cursor.getColumnIndex("_id"));

    TableLayout table;
    final int row = 3; 
    final CheckBox cb[] = new CheckBox[row];

    table = (TableLayout) findViewById(R.id.tablelayout);

    for (int i = 0; i < row; i++) {
        TableRow rows = new TableRow(this);
        final CheckBox pass = new CheckBox(this);
        pass.setGravity(Gravity.CENTER);
        pass.setTextColor(Color.BLACK);
        pass.setText("a" + id);

        rows.addView(pass);

        View view = new View(this);
        view.setLayoutParams(new 
       TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, 2));
        view.setBackgroundColor(Color.BLACK);
        table.addView(rows);
        table.addView(view);
        cb[i] = pass;
    }

    smsbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            for (int a = 0; a < row; a++) {
                if (cb[a].isChecked()) {
                    Message.message(getApplicationContext(), "Checked :" + cb[a].getText());
                }
            }
        }
    });

               }

           }

展示台样本

标签: javaandroidsqlite

解决方案


我搞定了。for 循环和 while 循环一起使循环增加了行的创建。所以我只使用 for 循环从 sqlite 列中进行选择并创建表行。谢谢大家。我的工作代码如下。

    SQLiteDatabase db = dataHelper.getReadableDatabase();
    String selectQuery = "SELECT * FROM " + dbSMS.TABLE_TALERT + " where _id > 1";
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.getCount() > 0) {
    TableLayout table;
    final int row = cursor.getCount(); //variable to get the number of row
    final CheckBox cb[] = new CheckBox[row];

    table = (TableLayout) findViewById(R.id.tablelayout);

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

        cursor.moveToNext();
        int id = cursor.getInt(cursor.getColumnIndex("_id"));

        TableRow rows = new TableRow(this);
        final CheckBox gateway = new CheckBox(this);
        gateway.setGravity(Gravity.CENTER);
        gateway.setTextColor(Color.BLACK);
        gateway.setText(String.valueOf(id));

        rows.addView(gateway);

        View view = new View(this);
        view.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, 2));
        view.setBackgroundColor(Color.BLACK);
        table.addView(rows);

        table.addView(view);

        cb[i] = gateway;

    smsbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            for (int a = 0; a < row; a++) {
                if (cb[a].isChecked()) {
                    Message.message(getApplicationContext(), "Checked :" + cb[a].getText());
                }
            }
        }
    });

               }

          }

推荐阅读