【问题标题】:Set the content of SQLite on Tablelayout checkbox and display the value of selected checkboxes在 Tablelayout 上设置 SQLite 的内容复选框并显示选中的复选框的值
【发布时间】:2021-01-16 07:55:27
【问题描述】:

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

我无法获得要显示的值。而 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());
                }
            }
        }
    });

               }

           }

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    我成功了。 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());
                    }
                }
            }
        });
    
                   }
    
              }
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多