title: Android Expandable ListView - parsed JSON response
body: <p>I'm trying to expand my app by offering an Expandable ListView for an existing ListView. The tutorials/answers I've found are often not well documented and don't examine cases where the data is dynamic (in my case a parsed JSON Array response from an API).</p>
  
  <p>I have two data classes: <code>SearchItem</code> and <code>InventoryItem</code>. I make a call to the API with Volley, and the JSON looks like this:</p>
  
  <pre><code>[
    {"product":"foobar", "image_url":"someurl", "inventory_items":
       [
          {"some_attribute":"some_value"},
          {"some_attribute":"some_value"},
          {"some_attribute":"some_value"}
       ]
    },
  
    {"product.......
  
  
  
    }
  ]
  </code></pre>
  
  <p>So far I've only parsed out the JSON for <code>SearchItems</code> like this:</p>
  
  <pre><code>private void parseJsonFeed(JSONArray response) {
          try {
  
              for (int i = 0; i &lt; response.length(); i++) {
                  JSONObject searchObj = (JSONObject) response.get(i);
  
                  SearchItem searchItem = new SearchItem();
                  searchItem.setId(searchObj.getInt("id"));
  
                  searchItem.setProduct(searchObj.getString("product"));
  
                  searchItem.setItemImg(searchObj.getString("img"));
  
                  searchItem.setId(searchObj.getInt("id"));
  
                  searchItem.setUpc(searchObj.getInt("upc"));
  
                  searchItems.add(searchItem);
  
              }
  
              // notify data changes to list adapater
              listAdapter.notifyDataSetChanged();
          } catch (JSONException e) {
              e.printStackTrace();
          }
  }
  </code></pre>
  
  <p>How do I now add child <code>InvetoryItems</code> to each <code>SearchItem</code> in my adapter? While parsing the response I can loop through each <code>InventoryItem</code>, but how do I add that as a child element of the corresponding <code>SearchItem</code>? Below is my existing extension of BaseAdapter:</p>
  
  <pre><code>public class SearchItemAdapter extends BaseAdapter {  
      private Activity activity;
      private LayoutInflater inflater;
      private List&lt;SearchItem&gt; searchItems;
      ImageLoader imageLoader = AppController.getInstance().getImageLoader();
  
      public SearchItemAdapter(Activity activity, List&lt;SearchItem&gt; searchItems) {
          this.activity = activity;
          this.searchItems = searchItems;
      }
  
      @Override
      public int getCount() {
          return searchItems.size();
      }
  
      @Override
      public Object getItem(int location) {
          return searchItems.get(location);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          if (inflater == null)
              inflater = (LayoutInflater) activity
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          if (convertView == null)
              convertView = inflater.inflate(R.layout.search_item, null);
  
          if (imageLoader == null)
              imageLoader = AppController.getInstance().getImageLoader();
  
          TextView product = (TextView) convertView.findViewById(R.id.product);
  
          TextView upc = (TextView) convertView.findViewById(R.id.upc);
  
          NetworkImageView itemImg = (NetworkImageView) convertView
                  .findViewById(R.id.itemImg);
  
          SearchItem searchItem = searchItems.get(position);
  
          // product info
          product.setText(searchItem.getProduct());
  
          // item pic
          itemImg.setImageUrl(searchItem.getItemImg(), imageLoader);
  
          upc.setText(String.valueOf(searchItem.getUpc()));
  
          return convertView;
      }
  }
  </code></pre>