如何从android中的动态CheckBoxes和RadioButtons获取值

时间:2017-01-11 08:46:14

标签: android android-checkbox android-radiobutton

根据我正在创建动态布局,我正在接受Json的回应。我可以创建动态复选框和单选按钮但是,我无法从这些动态复选框和单选按钮中获取值。我试图在Button上点击CheckBox的实例,但无法获取值。我获取值的唯一时间是在CheckBoxes上设置checkedchangelistener。如何从CheckBoxes和RadioButtons获取值?

[
  {
    "QId": 15,
    "Questions": "Have you taken any medication?",
    "InputType": "cb",
    "NoOfInputs": "2",    
    "QuestionOptions": [
      "Yes",
      "No"
    ]
  },
  {
    "QId": 16,
    "Questions": "Test Question for a radio button",
    "InputType": "rb",
    "NoOfInputs": "4",
    "QuestionOptions": [
      "Yes",
      "No",
      "May be",
      "May not be"
    ]
  }
]
public class NextActivity extends AppCompatActivity{

    private Button button;
    private LinearLayout linear, newcbll;
    private Model model;
    private LinkedHashMap<String,String> mappy = new LinkedHashMap<>();
    private ArrayList<Model> arraylist = new ArrayList<>();
    private ArrayList<DynamicModel> dynamolist = new ArrayList<>();
    private ArrayList<String> optionlist = new ArrayList<>();
    private static int arraycout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainact);

        arraycout = 0;

        linear = (LinearLayout)findViewById(R.id.linear);
        button = (Button)findViewById(R.id.button);
        GetAllQuestion();

        model = new Model();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("sammy_total_count: "+arraycout);
                for(int i=0; i<arraycout; i++) {
                    //Model model = arraylist.get(i);
                    //DynamicModel dynamo = new DynamicModel();
                    View nextChild = newcbll.getChildAt(i);

                   if(nextChild instanceof CheckBox){
                       CheckBox check = (CheckBox)nextChild;
                       if(check.isChecked()){
                           /*optionlist.add(check.getText().toString());
                           dynamo.setQoptions(optionlist);*/

                           System.out.println("sammy_Checkbox_values: "+check.getText().toString());
                       }
                   }

                }
            }
        });

    }

    private void GetAllQuestion(){
        StringRequest stringRequest = new StringRequest(Request.Method.GET, "My_Url", new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jarr = new JSONArray(response);
                    for (int i = 0; i < jarr.length(); i++) {
                        JSONObject ja = jarr.getJSONObject(i);
                        String QId = ja.getString("QId");
                        String Questions = ja.getString("Questions");
                        String InputType = ja.getString("InputType");
                        String NoOfInputs = ja.getString("NoOfInputs");
                        JSONArray Qoption = ja.getJSONArray("QuestionOptions");
                        if(Qoption != null && Qoption.length() > 0 ){
                            for(int j=0; j<Qoption.length(); j++){
                                mappy.put(String.valueOf(j), Qoption.getString(j));
                            }

                        }
                        model = new Model();
                        model.setQId(QId);
                        model.setQuestions(Questions);
                        model.setInputType(InputType);
                        model.setNoOfInputs(NoOfInputs);
                        model.setMamppy(mappy);
                        arraycout=jarr.length();
                        arraylist.add(model);

                        if(InputType.equals("cb")){
                            Set<?> set = mappy.entrySet();
                            Iterator<?> iterator = set.iterator();
                            while (iterator.hasNext()) {
                                Map.Entry me = (Map.Entry) iterator.next();
                                /*System.out.print(me.getKey() + ": ");
                                System.out.println(me.getValue());*/
                                newcbll = new LinearLayout(NextActivity.this);
                                LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                newcbll.setLayoutParams(lparams);
                                CheckBox checkBox = new CheckBox(NextActivity.this);
                                LinearLayout.LayoutParams checkparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                checkBox.setLayoutParams(checkparams);
                                checkBox.setId(Integer.parseInt(me.getKey().toString()));
                                checkBox.setText(me.getValue().toString());
                                checkBox.setOnCheckedChangeListener(handleCheck(checkBox));
                                newcbll.addView(checkBox);
                                linear.addView(newcbll);

                            }


                        }

                    }




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

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }


        });

        RequestQueue requestQueue = Volley.newRequestQueue(NextActivity.this);
        requestQueue.add(stringRequest);
    }

    private CompoundButton.OnCheckedChangeListener handleCheck (final CheckBox chk){
        return new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    System.out.println("sammy_cblistener "+buttonView.getText());
                }
            }
        };
    }
}

0 个答案:

没有答案