dimanche 16 avril 2017

Getting CheckBox id and text when dynamically created using for loop

I have a Button, questions and multiple checkBoxes created dynamically using for loop that looked like thispic

This is how i get all the data and how i create the question as well as checkBoxes

try {
JSONArray jsonArray = response.getJSONArray("values");

for (int i = 0; i < jsonArray.length(); i++) {
 JSONObject category = jsonArray.getJSONObject(i);
 categoryTxt = category.getString("category");
 Log.v("category", categoryTxt);

 //create Question
 TextView title = new TextView(FactorsActivity.this);
 title.setTextSize(20);
 title.setId(i);
 title.setText(categoryTxt);
 title.setTextColor(Color.BLUE);
 mLinearLayout.addView(title);

 JSONArray factors = category.getJSONArray("factors");

 Log.v("size factors", String.valueOf(factors.length()));
 nameArray = new String[factors.length()];
 for(int j = 0; j < factors.length(); j++)
 {
  JSONObject factorsObj = factors.getJSONObject(j);
  id = factorsObj.getString("id");
  //Log.v("id", id);
  name = factorsObj.getString("name");
  // Log.v("name", name);

  nameArray[j] = name;

  checkBox = new CheckBox(FactorsActivity.this);
  checkBox.setId(Integer.parseInt(id));
  checkBox.setText(nameArray[j]);
  mLinearLayout.addView(checkBox);

 }
}

Now my main objective is, If let say I checked is daily box, and I click submit. When i click submit, I want to have all of the selected checkbox information such as the question, its id and its text. The information here should be

  1. Question: recurrence of headache
  2. checkbox ID: number
  3. checkbox Text: is daily Many thanks.



Aucun commentaire:

Enregistrer un commentaire