【发布时间】: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());
}
}
}
});
}
}
【问题讨论】: