Reputation: 161
i am creating checkbox dynamically from a list ,now how to check user selected which checkbox from a list in android
for (int i = 0; i < languageList.size(); i++) {
HashMap<String, String> item_hash = new HashMap<String, String>();
String lang = languageList.get(i).getLanguage();
final String langId = languageList.get(i).getLanguageId();
item_hash.put(langId, lang);
ar[i] = langId;
// String langId = languageList.get(i).getLanguage_id();
item_list1.add(item_hash);
cb = new CheckBox(parentActivity);
cb.setText(languageList.get(i).getLanguage().toString());
cb.setId(Integer.parseInt(languageList.get(i).getLanguageId()));
Button btnSubmit = (Button) findViewById(R.id.btn_submit);
// show location button click event
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(cb.isChecked()){
System.out.println("id "+cb.getText()+langId);
}
}
});
}
Upvotes: 1
Views: 258
Reputation: 9103
You just have to add a unique tag to every CheckBox
and get it inside the CheckListener along with the declaration like the following way:
// at the class declaration define the following static element
static int cb_selected;
// then inside the for loop
cb.setTag(i);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
cb_selected = Integer.parseInt(buttonView.getTag().toString().trim());
}
});
And if you want to save all checked checkboxes
:
// before the for loop
final List<String> selected = new ArrayList<>();
// then inside the for loop
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
selected.add(buttonView.getTag().toString().trim());
else
selected.remove(buttonView.getTag().toString().trim());
}
});
So, when trying to see what checked, just look at this ArrayList
.
Upvotes: 0
Reputation: 301
Maybe it would be better if you should use a ListView. But if you prefer use your way you need and checkbox array
ArrayList<CheckBox> cbs = new ArrayList<Checkbox>();
for (int i = 0; i < languageList.size(); i++)
{
HashMap<String, String> item_hash = new HashMap<String, String>();
String lang = languageList.get(i).getLanguage();
final String langId = languageList.get(i).getLanguageId();
item_hash.put(langId, lang);
ar[i] = langId;
// String langId = languageList.get(i).getLanguage_id();
item_list1.add(item_hash);
cb = new CheckBox(parentActivity);
cb.setText(languageList.get(i).getLanguage().toString());
cb.setId(Integer.parseInt(languageList.get(i).getLanguageId()));
cbs.add(cb);
}
Button btnSubmit = (Button) findViewById(R.id.btn_submit);
// show location button click event
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
for (int i = 0; i < languageList.size(); i++)
if(cbs.get(i).isChecked())
System.out.println("id "+cbs.get(i).getText()+langId);
}
});
Upvotes: 2