I automatically create a check box in a list, now, how to check the selected user, which check box of a list in Android

advertisements

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);

            }

        }
    });
}


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);

    }
});