mardi 28 mars 2017

Check if various checkboxes that have being created programmatically are checked when you click a button in android

My question is: How I can check if the checkboxes are checked and how can I get its id or text in a onclick event? I have done it whit RadioGroup, but RadioGroup only let me check 1 RadioButton, and in this case I need to check more than one, but if Im wrong and there is a way to do it with RadioGroup it should work for me. Thanks for the help Here is the code where I create the checkboxes programmatically:

protected void onCreate(Bundle savedInstanceState) {

    LinearLayout lg = (LinearLayout) findViewById(R.id.lg);
    Button botonenv = (Button) findViewById(R.id.botonenv);


    try{
        myJson = new JSONObject(data);

        JSONArray Solutions = myJson.getJSONArray("solutions");


        for (int i = 0; i < Solutions.length(); i++) {

            JSONObject JSonSol = Solutions.getJSONObject(i);
            final String idSt = JSonSol.getString("id");
            final String name = JSonSol.getString("name");
            String resp = name.toUpperCase();


            CheckBox cb1 = new CheckBox(this);

            cb1.setText(resp);

            LinearLayout.LayoutParams llg = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            llg.setMargins(0, 30, 0, 0);


            cb1.setTextSize(15);
            cb1.setTextColor(Color.parseColor("#FF000000"));


            lg.addView(cb1);


            int id =Integer.parseInt(idSt);
            cb1.setId(id);


        }



    }catch(JSONException e){
        e.printStackTrace();
    }

        botonenv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           //HERE I MUST NOW WHAT CHECKBOXES ARE CHECKED AND THEIR IDS

        ..............
        }           
    });


}




Aucun commentaire:

Enregistrer un commentaire