lundi 17 octobre 2016

use .getId() to get id and use the id to find the element android

if (className.equalsIgnoreCase("android.widget.CheckBox")) {
  CheckBox value = (CheckBox) obj3;//where obj is the checkbox view
  int parentId = ((View) value.getParent()).getId();
  RadioGroup parent = (RadioGroup) findViewById(parentId);
  int checkboxnumber = parent.getChildCount();
  for (int p = 0; p < checkboxnumber; p++) {
    if (value.isChecked()) {
      String answer = value.getText().toString();
      Integer answerid = value.getId();
      //System.out.println("TextView touchable "+answer);
      j.put("answer", answer);
    }
  }

  if (j.length() > 0) {
    myArray.put(j);
  }
}

I have a radiogroup when I append checkboxes. I want to get the total of checked checkbox on a radiogroup based on one checkbox that is checked.

What I did is get the parent id of the checkbox using .getId() after getting the id I use it to get the parent id element using findViewById then to get the checked checkbox I use getChildCount()

But I this error :

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getChildCount()' on a null object reference

How to use view to get parent id to use the id to count the checkbox?




Aucun commentaire:

Enregistrer un commentaire