我正在从列表中动态创建复选框,现在如何检查用户从 android 中的列表中选择了哪个复选框
i am creating checkbox dynamically from a list ,now how to check user selected which checkbox from a list in android
我正在从列表中动态创建复选框,现在如何检查用户从 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);
}
}
});
}
如果您使用 ListView 可能会更好。但是如果你喜欢使用你需要的方式和复选框数组
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);
}
});
您只需为每个 CheckBox
添加一个唯一的标签,并将其与声明一起放入 CheckListener 中,如下所示:
// 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());
}
});
如果你想保存所有勾选的 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());
}
});
所以,当试图查看选中的内容时,只需查看此 ArrayList
。
我正在从列表中动态创建复选框,现在如何检查用户从 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);
}
}
});
}
如果您使用 ListView 可能会更好。但是如果你喜欢使用你需要的方式和复选框数组
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);
}
});
您只需为每个 CheckBox
添加一个唯一的标签,并将其与声明一起放入 CheckListener 中,如下所示:
// 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());
}
});
如果你想保存所有勾选的 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());
}
});
所以,当试图查看选中的内容时,只需查看此 ArrayList
。