title: Tree with checkBox
body: <p>Good day!
  It was necessary to create a list-tree, approximately, as represented in the picture:</p>
  
  <p><a href="https://joshsmithonwpf.wordpress.com/2008/08/01/article-about-checkboxes-in-a-wpf-treeview/" rel="nofollow noreferrer">https://joshsmithonwpf.wordpress.com/2008/08/01/article-about-checkboxes-in-a-wpf-treeview/</a></p>
  
  <p>Forgive me for this, the site itself does not allow me to insert the picture</p>
  
  <p>The tree consists of a check of the boxes, when you click on the parent, the checkboxes are displayed / removed from the child elements. In addition to the checkboxes, each element of the tree has its own parameter (byte array).</p>
  
  <p>Actually, it is necessary to read the state of the checkboxes after pressing the checkboxes (by clicking, say, a button), and taking the values ​​(the same array of bytes), the name, the name of the tab and writing it to another array / list, etc.</p>
  
  <p>The main question is how to create a TreeView where the state of the child elements depends on the parent and how to assign them, among other things, also the values.</p>
  
  <p>I saw many examples, but I did not find it. Just come across the broken links.</p>
  
  <p>I could implement only the checkbox list on my own, but the child elements do not react to changing the state of the parent.</p>
  
  <p>The code is shown below.</p>
  
  <p>ExpandableListAdapter.java</p>
  
  <pre><code>    public class ExpandableListAdapter extends BaseExpandableListAdapter {
  
      private Context context;
      private List&lt;String&gt; listDataHeader;
      private HashMap&lt;String, List&lt;String&gt;&gt; listDataChild;
  
      public ExpandableListAdapter(Context context, List&lt;String&gt; listHeaderData, HashMap&lt;String, List&lt;String&gt;&gt; listChildData){
          this.context = context;
          this.listDataHeader = listHeaderData;
          this.listDataChild = listChildData;
      }
  
      @Override
      public int getGroupCount() {
          return this.listDataHeader.size();
      }
  
      @Override
      public int getChildrenCount(int groupPosition) {
          return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
      }
  
      @Override
      public Object getGroup(int groupPosition) {
          return this.listDataHeader.get(groupPosition);
      }
  
      @Override
      public Object getChild(int groupPosition, int childPosition) {
          return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition);
      }
  
      @Override
      public long getGroupId(int groupPosition) {
          return groupPosition;
      }
  
      @Override
      public long getChildId(int groupPosition, int childPosition) {
          return childPosition;
      }
  
      @Override
      public boolean hasStableIds() {
          return false;
      }
  
      @Override
      public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  
          String headerTitle = (String) getGroup(groupPosition);
          if (convertView == null) {
              LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = infalInflater.inflate(R.layout.group_row, null);
          }
  
          TextView lblListHeader = (TextView) convertView
                  .findViewById(R.id.lblListHeader);
          lblListHeader.setTypeface(null, Typeface.BOLD);
          lblListHeader.setText(headerTitle);
  
          return convertView;
      }
  
  
      @Override
      public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  
          final String childText = (String) getChild(groupPosition, childPosition);
  
          if(convertView == null){
              LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = inflater.inflate(R.layout.child_row, null);
          }
          TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
  
          txtListChild.setText(childText);
  
          return convertView;
      }
  
      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
          return true;
      }
  }
  </code></pre>
  
  <p>child_row.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="55dip"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="5dp"
      android:layout_marginRight="5dp"
      android:orientation="horizontal"&gt;
  
      &lt;TextView
          android:id="@+id/lblListItem"
          android:layout_width="308dp"
          android:layout_height="match_parent"
          android:paddingBottom="5dp"
          android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
          android:paddingTop="5dp"
          android:text="asd"
          android:textSize="20dip" /&gt;
  
      &lt;CheckBox
          android:id="@+id/lblListChildCheckbox"
          android:layout_width="72dp"
          android:layout_height="match_parent"
          android:focusable="false"
          android:textSize="17dp" /&gt;
  
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>group_row.xml</p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dp"
          android:layout_marginLeft="5dp"
          android:layout_marginRight="5dp"
          android:orientation="horizontal"
          android:padding="8dp"&gt;
  
          &lt;TextView
              android:id="@+id/lblListHeader"
              android:layout_width="306dp"
              android:layout_height="match_parent"
              android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
              android:textSize="20dp" /&gt;
  
          &lt;CheckBox
              android:id="@+id/lblListHeaderCheckbox"
              android:layout_width="50dp"
              android:layout_height="match_parent"
              android:focusable="false"
              android:text=""
              android:textSize="17dp" /&gt;
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>listings.xml</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="utf-8"?&gt;
  
      &lt;LinearLayout xmlns:andoid="http://schemas.android.com/apk/res/android"
          andoid:layout_width="match_parent"
          andoid:layout_height="match_parent"
          andoid:orientation="vertical"&gt;
  
          &lt;Button
              andoid:id="@+id/add_template"
              style="@style/Widget.AppCompat.Button.Colored"
              andoid:layout_width="match_parent"
              andoid:layout_height="wrap_content"
              andoid:layout_gravity="center"
              andoid:layout_marginTop="10dp"
              andoid:text="Добавить шаблон" /&gt;
  
          &lt;ExpandableListView
              andoid:id="@+id/listing"
              andoid:layout_width="match_parent"
              andoid:layout_height="wrap_content"&gt;&lt;/ExpandableListView&gt;
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Choise_list.java</p>
  
  <pre><code>public class Choise_List extends Activity {
  
      private Button add_template;
  
      class Lists{
          String page;
          String name;
          String type;
          byte[] val;
      }
  
      Context context;
  
      ExpandableListAdapter listAdapter;
      ExpandableListView expListView;
      HashMap&lt;String, List&lt;String&gt;&gt; listDataChild;
      List&lt;String&gt; pages;
  
      ArrayList&lt; Map&lt;String, String&gt; &gt; childDataItem = new ArrayList&lt;&gt;();
  
      List&lt;Lists&gt; structure_list = new ArrayList&lt;&gt;();
  
      public void createListsList(ArrayList&lt;byte[]&gt; ch_check) throws UnsupportedEncodingException {
          for (int i = 0; i &lt; ch_check.size(); i++){
              if(Arrays.equals( ch_check.get(i), "PAGE".getBytes( Charset.forName("UTF-8") ) ) ){
                  Lists lists = new Lists();
                  int state = i;
                  lists.page = new String( ch_check.get(++state), "UTF-8" );
                  lists.name = new String( ch_check.get(++state), "UTF-8" );
                  lists.type = new String( ch_check.get(++state), "UTF-8" );
                  lists.val = ch_check.get(++state);
  
                  i = state;
                  structure_list.add(lists);
              }
          }
      }
  
      private void prepareListData(){
  
          HashSet&lt;String&gt; used = new HashSet&lt;&gt;();
  
          int count = 0;
  
          pages = new ArrayList&lt;&gt;();
          listDataChild = new HashMap&lt;String, List&lt;String&gt;&gt;();
  
          for(int i = 0; i &lt; structure_list.size(); i++){
              if(used.contains(structure_list.get(i).page)){
                  continue;
              } else {
                  used.add(structure_list.get(i).page);
              }
  
              String new_page_name = "";
              ArrayList&lt;Integer&gt; positions = new ArrayList&lt;&gt;();
  
              positions.add(i);
              for(int j = i + 1; j &lt; structure_list.size(); j++){
  
                  if(structure_list.get(i).page.equals(structure_list.get(j).page)){
                      positions.add(j);
                  }
              }
  
              childDataItem = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
  
              List&lt;String&gt; child = new ArrayList&lt;String&gt;();
  
              for(Integer p : positions){
                  new_page_name = structure_list.get(p).page;
                  child.add(structure_list.get(p).name);
              }
              pages.add(new_page_name);
              listDataChild.put(pages.get(count), child);
              count++;
          }
  
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.listings);
  
  
          expListView = (ExpandableListView) findViewById(R.id.listing);
  
          ArrayList&lt;byte[]&gt; ch_check = new ArrayList&lt;&gt;();
  
          if(savedInstanceState == null){
              MyObject myObj = (MyObject) getIntent().getParcelableExtra(MyObject.class.getCanonicalName());
  
              if(myObj == null){
                  ch_check = null;
              } else{
                  ch_check = myObj.choise;
              }
          }
  
          if(ch_check != null){
  
              try {
                  createListsList(ch_check);
              } catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
              }
  
              prepareListData();
  
              expListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
              listAdapter = new ExpandableListAdapter(this, pages, listDataChild);
              expListView.setAdapter(listAdapter);
  
              expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                  @Override
                  public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
  
                      return false;
                  }
              });
  
              expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                  @Override
                  public void onGroupExpand(int groupPosition) {
                      Toast.makeText(getApplicationContext(), pages.get(groupPosition) + " Expanded", Toast.LENGTH_SHORT).show();
                  }
              });
  
              expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
  
                  @Override
                  public void onGroupCollapse(int groupPosition) {
                      Toast.makeText(getApplicationContext(),pages.get(groupPosition) + " Collapsed", Toast.LENGTH_SHORT).show();
                  }
              });
  
              expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  
                  @Override
                  public boolean onChildClick(ExpandableListView parent, View v,
                                              int groupPosition, int childPosition, long id) {
                      // TODO Auto-generated method stub
                      Toast.makeText( getApplicationContext(),pages.get(groupPosition) + " : " + listDataChild.get(pages.get(groupPosition)).get( childPosition), Toast.LENGTH_SHORT).show();
                      CheckBox checkBox = (CheckBox) findViewById(R.id.lblListChildCheckbox);
                      checkBox.setChecked(true);
                      checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                          @Override
                          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                              checkBox.setChecked(isChecked);
                          }
                      });
                      return false;
                  }
              });
          }
  
      }
  </code></pre>