title: ExpandableListView: Get input values from childs
body: <p>I'm parsing a JSON into a ExpandableListView, on each child the user can select the amount of each child he wants to have due +/- Buttons. The +/- Buttons are connected to a TextView where the total amount of each child gets displayed and the total cost will be displayed at the end of the line.</p>
  
  <p>At the Bottom of the parent there should be a TextView with the summary of all the values calculated in every child of the ExpListView (Summary)
  And the OK Button at the Bottom should send the amount of each child to the server (the amount is connected to a ID).</p>
  
  <p>I'm having problems with reading out the amount of each child when I'm clicking on the "OK" Button - how can I build the bridge to the values of my Childs?</p>
  
  <p>I also encounter problems reading out the cost of each childto calculate the total cost at the bottom. The onClickListener in the Child should somehow refresh the TextView at the bottom, but as far as I know, that's not going to be easy, right?</p>
  
  <p>Has anyone an idea how to access the values?</p>
  
  <p>This is the ChildView of my ListAdapter where the magic happens:</p>
  
  <pre><code>public class ExpandableListAdapterDrinks extends BaseExpandableListAdapter {
  private Context context;
  private List&lt;String&gt; listDataHeader,listDataHeaderPrice;
  private HashMap&lt;String,List&lt;String&gt;&gt; listHashMap;
  private List&lt;Drink&gt; drinksList;
  
  
  class ViewHolder {
      TextView childText,counterText, childUnitPrice, childFinalPrice;
      Button btn_plus,btn_minus;
  
  }
  
  class DrinksListChildItem{
      String name;
      int quantity;
      DrinksListChildItem(String name, int quantity){
          this.name = name;
          this.quantity = quantity;
      }
  }
  
  class Pos{
      int group;
      int child;
      Pos(int group, int child){
          this.group = group;
          this.child = child;
      }
  }
  
  
  
  
  public ExpandableListAdapterDrinks(Context context, List&lt;Drink&gt; drinksList) {
      this.context = context;
      this.drinksList= drinksList;
  
    }
  
  
  
  
  @Override
  public int getGroupCount() {
      return drinksList.size();
  }
  
  
  @Override
  public int getChildrenCount(int groupPosition) {
  
      return drinksList.get(groupPosition).getContent().size();
  
  }
  
  @Override
  public Object getGroup(int groupPosition) {
      return drinksList.get(groupPosition);
  }
  
  
  @Override
  public Object getChild(int groupPosition, int childPosition) {
      return drinksList.get(groupPosition).getContent();
       listHashMap.get(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(final int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
      String headerTitle = (String) drinksList.get(groupPosition).getTitle();
  
      /** HEADER TEXT HERE */
  
  
  
      if(view==null) {
          LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          view = inflater.inflate(R.layout.vip_package_listgroup,null);
      }
      final LinearLayout bgcolor = view.findViewById(R.id.lblListHeaderLayout);
      TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
      TextView lblListHeaderPrice = (TextView)view.findViewById(R.id.lblListHeader_Price);
      Button lblListHeaderButton = view.findViewById(R.id.lblListHeaderButton);
  
      lblListHeaderPrice.setVisibility(View.GONE);
      lblListHeaderButton.setVisibility(View.GONE);
  
      if(!drinksList.get(groupPosition).getBg().get(0).isEmpty() || !drinksList.get(groupPosition).getBg().get(1).isEmpty() ) {
  
          List&lt;String&gt; colorGradientTopBottom = drinksList.get(groupPosition).getBg();
  
          int[] colors = {Color.parseColor(colorGradientTopBottom.get(0)),Color.parseColor(colorGradientTopBottom.get(1))};
          GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
          gd.setCornerRadius(0f);
          bgcolor.setBackground(gd);
      } else {
  
          bgcolor.setBackgroundColor(ContextCompat.getColor(context, R.color.cardview_bg));
      }
  
      lblListHeader.setText(headerTitle);
  
      return view;
  
  }
  
  @Override
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
  
      /** Drinks List */
      final ViewHolder viewHolder;
  
      final List&lt;String&gt; childDrink = drinksList.get(groupPosition).getContent();
      final List&lt;Integer&gt; childPrice = drinksList.get(groupPosition).getPricelist();
      //final String childText =  childDrink.get(childPosition);
  
      if(view == null) {
          LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          view = inflater.inflate(R.layout.vip_drinks_listitem,null);
  
          final TextView txtListChild = view.findViewById(R.id.lblListItemDrinks);
          final TextView txtListDrinksUnitPrice = view.findViewById(R.id.lblListItemDrinksUnitPrice);
          final TextView txtDrinksAmount = view.findViewById(R.id.vip_drinks_amount);
          final TextView txtListDrinkPriceFinal = view.findViewById(R.id.lblListItemDrinksFinalPrice);
          Button btn_plus = view.findViewById(R.id.vip_drinks_btn_plus);
          Button btn_minus = view.findViewById(R.id.vip_drinks_btn_minus);
  
          viewHolder = new ViewHolder();
          viewHolder.childText = txtListChild;
          viewHolder.childUnitPrice = txtListDrinksUnitPrice;
          viewHolder.counterText = txtDrinksAmount;
          viewHolder.childFinalPrice = txtListDrinkPriceFinal;
          viewHolder.btn_plus = btn_plus;
          viewHolder.btn_minus = btn_minus;
  
  
          viewHolder.childText.setText(childDrink.get(childPosition));
          viewHolder.counterText.setText("0");
          viewHolder.childFinalPrice.setText("0");
          final float headerPrice = childPrice.get(childPosition);
          final int headerPriceInt = (int) headerPrice;
          viewHolder.childUnitPrice.setText(String.format("%.02f",headerPrice).replace(".",",") +"€");
  
          DrinksListChildItem child = childDrink.get(childPosition);
  
  
          viewHolder.btn_plus.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  int t = Integer.parseInt(viewHolder.counterText.getText().toString());
                  ChildItem selectedItem = viewHolder.counterText.getText().toString();
                  selectedItem.quantity = selectedItem.quantity+1;
                  notifyDataSetChanged();
  
                  viewHolder.counterText.setText(String.valueOf(t + 1));
                  viewHolder.childFinalPrice.setText(String.valueOf((t+1)* headerPriceInt) + "€");
              }
          });
  
  
  
          viewHolder.btn_minus.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Pos pos = (Pos) v.getTag();
                  int t = Integer.parseInt(viewHolder.counterText.getText().toString());
                  DrinksListChildItem selectedItem = (DrinksListChildItem) getChild(pos.group,pos.child);
                  selectedItem.quantity = selectedItem.quantity-1;
                  notifyDataSetChanged();
                  viewHolder.counterText.setText(String.valueOf(t - 1));
                  viewHolder.counterText.setText(String.valueOf((t-1)* headerPriceInt) + "€");
              }
          });
  
  
      } else {
          viewHolder = (ViewHolder) view.getTag();
      }
  
  
      return view;
  </code></pre>
  
  <p>I'm having massive problems glueing your code to my Code (expand problem with a TextView): I don't understand exactly how to connect the ChildItem child = childDrink.get(childPosition); to my drinksList to make the setOnCLickListener increase the selectedItem.quantity. The Code works somehow, but it messes up the order of my childs and it also selects the quantity in the other childs</p>
  
  <p>Drinks (Pojo)</p>
  
  <pre><code>public class Drink {
  @SerializedName("title")
  @Expose
  private String title;
  @SerializedName("bg")
  @Expose
  private List&lt;String&gt; bg = null;
  @SerializedName("content")
  @Expose
  private List&lt;String&gt; content = null;
  @SerializedName("pricelist")
  @Expose
  private List&lt;Integer&gt; pricelist = null;
  
  public String getTitle() {
      return title;
  }
  
  public void setTitle(String title) {
      this.title = title;
  }
  
  public List&lt;String&gt; getBg() {
      return bg;
  }
  
  public void setBg(List&lt;String&gt; bg) {
      this.bg = bg;
  }
  
  public List&lt;String&gt; getContent() {
      return content;
  }
  
  public void setContent(List&lt;String&gt; content) {
      this.content = content;
  }
  
  public List&lt;Integer&gt; getPricelist() {
      return pricelist;
  }
  
  public void setPricelist(List&lt;Integer&gt; pricelist) {
      this.pricelist = pricelist;
  }
  
  }
  </code></pre>
  
  <p>VipDrinks(Pojo):
      public class VipDrinks {</p>
  
  <pre><code>@SerializedName("drinks")
  @Expose
  private List&lt;Drink&gt; drinks = null;
  
  public List&lt;Drink&gt; getDrinks() {
      return drinks;
  }
  
  public void setDrinks(List&lt;Drink&gt; drinks) {
      this.drinks = drinks;
  }
  
  }
  </code></pre>
  
  <p>JSON has the following structure:</p>
  
  <pre><code>{
   "drinks":[ {
                "title":,
                "bg":[],
                "content":[],
                "pricelist":[],
               },
              {...} 
            ]
   }
  </code></pre>
  
  <p>Here is the Activity with the Parsing Request:</p>
  
  <pre><code>public class drinks_selection extends AppCompatActivity {
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_drinks_selection);
  
          final ExpandableListView listView = findViewById(R.id.vip_drinks_listview);
  
  
          /** PARSING JSON FROM SERVER */
  
          final JsonObjectRequest galleryUrls = new JsonObjectRequest(Request.Method.GET, PARSE_URL, null, new Response.Listener&lt;JSONObject&gt;() {
              @Override
              public void onResponse(JSONObject response) {
                  try {
                      JSONArray packageArray = response.getJSONArray("drinks");
                      Log.e("response", String.valueOf(response));
                      GsonBuilder gsonBuilder = new GsonBuilder();
                      Gson gson = gsonBuilder.create();
                      List&lt;Drink&gt; vipDrinks = Arrays.asList(gson.fromJson(String.valueOf(packageArray),Drink[].class));
                      List&lt;Drink&gt; arrayList = new ArrayList&lt;&gt;(vipDrinks);
  
                      ExpandableListAdapterDrinks listAdapter = new ExpandableListAdapterDrinks(getApplicationContext(),arrayList);
                      listView.setAdapter( listAdapter);
                      listView.expandGroup(0);
  
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
  
              }
          }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
  
  
                  Log.e("ERROR", String.valueOf(error));
              }
          });
  
          RequestQueue rQ = Volley.newRequestQueue(this);
          rQ.add(galleryUrls);
  
      }
  }
  </code></pre>
  
  <p>VIP Package List Group.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      android:id="@+id/lblListHeaderLayout"
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="@dimen/default_padding"
      android:background="@drawable/bg_vip_booking"
      android:orientation="horizontal"
  
      &gt;
  
  &lt;RelativeLayout
  
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:paddingStart="?android:attr/expandableListPreferredItemPaddingLeft"
      android:paddingEnd="@dimen/feed_item_padding_left_right"
      android:layout_weight="0.9"
      &gt;
  
      &lt;TextView
          android:id="@+id/lblListHeader"
          android:textSize="@dimen/text_title_list_header"
          android:textColor="@color/Textwhite"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:fontFamily="@font/mont_bold"
  
          /&gt;
  
      &lt;TextView
          android:id="@+id/lblListHeader_Price"
          android:textSize="@dimen/text_title_list_header"
          android:textColor="@color/Textwhite"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentEnd="true"
          android:fontFamily="@font/mont_bold"
  
          /&gt;
  &lt;/RelativeLayout&gt;
  
      &lt;Button
          android:id="@+id/lblListHeaderButton"
          android:layout_width="60dp"
          android:layout_height="30dp"
          android:background="@drawable/bg_btn_ripple_dark"
          android:text="@string/btn_ok"
          android:textColor="@color/Textwhite"
          android:fontFamily="@font/mont_bold"
          android:focusable="false"
  
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>VIP Drinks ListItem.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="60dip"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      android:layout_centerInParent="true"
  
      &gt;
  
  
      &lt;TextView
          android:id="@+id/lblListItemDrinks"
          android:paddingTop="@dimen/padding_small"
          android:paddingBottom="@dimen/padding_small"
          android:textSize="@dimen/text_normal"
          android:paddingLeft="@dimen/default_padding"
          android:layout_width="150dp"
          android:layout_height="wrap_content"
          android:fontFamily="@font/mont_medium"
          android:textColor="@color/Textwhite"
          app:layout_constraintStart_toStartOf="parent"
  
  
          /&gt;
  
  
  
      &lt;RelativeLayout
          android:id="@+id/vip_drinks_layout"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_toEndOf="@id/lblListItemDrinks"
          android:paddingStart="@dimen/default_padding"
          android:layout_centerInParent="true"
          android:paddingEnd="@dimen/default_padding"
          app:layout_constraintEnd_toEndOf="parent"
  
  
  
          &gt;
  
          &lt;TextView
              android:id="@+id/lblListItemDrinksUnitPrice"
              android:textSize="@dimen/text_normal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:fontFamily="@font/mont_light"
              android:textColor="@color/Textwhite"
              android:paddingEnd="@dimen/padding_small"
              /&gt;
  
          &lt;Button
              android:id="@+id/vip_drinks_btn_minus"
              android:layout_toEndOf="@id/lblListItemDrinksUnitPrice"
  
              android:layout_width="30dp"
              android:layout_height="20dp"
              android:text="-"
  
              android:background="@drawable/bg_btn_ripple_dark"
              android:textColor="@color/white"
              android:textSize="14sp"
              android:layout_marginEnd="@dimen/padding_small"
  
              /&gt;
  
  
          &lt;TextView
              android:id="@+id/vip_drinks_amount"
              android:layout_toEndOf="@+id/vip_drinks_btn_minus"
  
  
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="@color/white"
              android:fontFamily="@font/mont_light"
              android:textSize="@dimen/text_normal"
              android:layout_marginEnd="@dimen/padding_small"
              /&gt;
  
  
          &lt;Button
              android:id="@+id/vip_drinks_btn_plus"
              android:layout_toEndOf="@+id/vip_drinks_amount"
  
              android:layout_width="30dp"
              android:layout_height="20dp"
              android:text="+"
              android:background="@drawable/bg_btn_ripple_dark"
              android:textColor="@color/white"
              android:textSize="14sp"
              android:layout_marginEnd="@dimen/padding_small"
  
              /&gt;
  
  
          &lt;TextView
              android:id="@+id/lblListItemDrinksFinalPrice"
              android:layout_toEndOf="@id/vip_drinks_btn_plus"
              android:textSize="@dimen/text_normal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:fontFamily="@font/mont_light"
              android:textColor="@color/Textwhite"
  
              /&gt;
      &lt;/RelativeLayout&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>