java - 在 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());
}
}
}
});
}
}
解决方案
我搞定了。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());
}
}
}
});
}
}
推荐阅读
- c++ - 攀登排行榜-黑客排名
- python - 为什么在使用 lxml 编写 XML 文件后,属性值中的非 ASCII 字符会转义?
- embedded - l3gd20 陀螺仪在 stm32f3discovery 上发回重复字节
- c++ - 使用 MinGW 在 Windows 上构建 GLEW 时遇到问题
- c# - 如何在场景之间保存firebase身份验证
- rust - 如何将 rustdoc 注释添加到 proc 宏生成的代码中?
- node.js - Docker / docker-compose 工作流程:未反映角度变化
- perl - 是否可以使用 perl 将鼠标光标移动到 xvfb 虚拟显示器?
- java - Android:从图库中选择照片
- python - 如何在 lmdb 中正确插入重复键?