Reputation: 134
I can dynamically add checkbox name in layout via java but how to add its key as we do in html? The data for the checkbox (cursor) are fetched from database, so i need to keep its id so that i can add the id of the selected checkbox as foreign key.
protected void onCreate(Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) findViewById(R.id.checkboxes);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
CheckBox cb = new CheckBox(this);
cb.setText(name);
layout.addView(cb);
}
}
Upvotes: 0
Views: 84
Reputation: 1143
You can make a workaround like this:
while (cursor.moveToNext()) {
final int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
CheckBox cb = new CheckBox(this);
cb.setText(name);
cb.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
yourMethodRequiredId(id, isChecked);
}
});
layout.addView(cb);
}
Upvotes: 1