【发布时间】:2018-04-26 05:52:52
【问题描述】:
我正在通过数据库中的记录动态创建复选框。我可以成功地为数据库中的每条记录生成 checkboxech。
比单击按钮后,我想根据选中的复选框在不同的表中添加一条记录。我需要检查所有复选框,看看它是否被选中,如果是,我需要将 id 传递给 id_participant 以创建记录。但我不知道该怎么做。
你能帮忙吗?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
myDB = new DBAdapter(getContext());
final View root = inflater.inflate(R.layout.fragment_add_from_db, container, false);
final LinearLayout layout = root.findViewById(R.id.check_add_layout);
btn_ad_group = root.findViewById(R.id.btn_add_group);
Bundle bundle = getArguments();
if (bundle != null) {
id_eventu = bundle.getLong("idevent");
}
myDB.open();
pocet = myDB.getParticipantsCount();
Cursor cursor = myDB.getAllRowsParticipant();
cursor.moveToFirst();
for(int i = 0; i < pocet; i++) {
CheckBox cb = new CheckBox(root.getContext());
String name = cursor.getString(cursor.getColumnIndex("name"));
String surname = cursor.getString(cursor.getColumnIndex("surname"));
String text = name + " " + surname;
cb.setText(text);
cb.setHeight(90);
cb.setId(cursor.getInt(cursor.getColumnIndex("_id")));
layout.addView(cb);
cursor.moveToNext();
}
myDB.close();
btn_ad_group.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myDB.open();
// here I want to write into database by selected checkboxes
// I want to read id of checked checkbox and pass it to id_participant
myDB.insertRowRegistr(id_eventu, id_participant);
myDB.close();
}
});
return root;
}
【问题讨论】:
标签: android sqlite checkbox android-sqlite