title: How do I get the selected item in a expandable RecyclerView
body: <p>I am new to android development and I started working on an expandable RecyclerView. Now My problem is that my child (sub-modules) list is populated dynamically from the selected parent (Module) on the expandable RecyclerView. I tried using a for loop but only the last child list is populated when I open. I was just wondering if there is a way I could implement an item click listener to just return the single selected module.</p>
  
  <p>Here is my adapter: </p>
  
  <pre><code>public class ReportAdapter extends ExpandableRecyclerViewAdapter&lt;ModuleViewHolder, DoiViewHolder&gt; {
  Context context;
  
  public ReportAdapter(List&lt;? extends ExpandableGroup&gt; groups, Context context) {
      super(groups);
      this.context = context;
  
  }
  
  @Override
  public ModuleViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_module,parent,false);
  
      return new ModuleViewHolder(view);
  }
  
  @Override
  public DoiViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_doi,parent,false);
  
      return new DoiViewHolder(view);
  }
  
  @Override
  public void onBindChildViewHolder(DoiViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
      ModuleDoi moduleDoi =(ModuleDoi) group.getItems().get(childIndex);
  
      holder.setDoiName(moduleDoi.getName());
  
  }
  
  @Override
  public void onBindGroupViewHolder(ModuleViewHolder holder, int flatPosition, ExpandableGroup group) {
  holder.setModuleName(group.getTitle());
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is my Parent Holder: </p>
  
  <pre><code>public class ModuleViewHolder extends GroupViewHolder implements View.OnClickListener{
  TextView moduleName ;
  
  public ModuleViewHolder(View itemView) {
      super(itemView);
  moduleName = (TextView) itemView.findViewById(R.id.module_name);
  
  
  }
  
    public void  setModuleName(String  name){
  
      moduleName.setText(name);
   }
  
  
   }
  </code></pre>
  
  <p>Here is my Childs holder (Sub Modules) </p>
  
  <pre><code>public class DoiViewHolder extends ChildViewHolder  {
  TextView doiName;
  public DoiViewHolder(View itemView) {
      super(itemView);
      doiName = (TextView) itemView.findViewById(R.id.module_doi_name);
  
  }
  public void setDoiName(String name){
      doiName.setText(name);
  }
  
  }
  </code></pre>
  
  <p>Here is where I set the adapter. I am using the 
  <code>moduleDoiList = fetchModuleMenus(APPID, MODULENAME);</code>
    fetch the  submodules list from a json file. the app id in this case is a constant and the MODULENAME is obtained from the Modules Array list.</p>
  
  <pre><code>   JSONArray jsonArray = jsonObject.getJSONArray("RESPONSE");
                              moduleDoiList = new ArrayList&lt;&gt;();
                              appModulesList = new ArrayList&lt;&gt;();
  
                              for (int i = 0; i &lt; jsonArray.length(); i++) {
                                  JSONObject eachModuleObject = jsonArray.getJSONObject(i);
                                  MODULES modules = new MODULES(eachModuleObject.getString("MODULENAME"),
                                          eachModuleObject.getString("MODULEID"));
                                  ModulesList.add(modules);
                                  for(int j = 0; j&lt;ModulesList.size(); j++) {
                                      String moduleTitle = ModulesList.get(i).getModuleName();
                                      String MODULENAME = ModulesList.get(i).getModuleName();
  
  
                                      moduleDoiList = fetchModuleMenus(APPID, MODULENAME);
                                      appModulesList.add(new AppModules(moduleTitle, moduleDoiList));
                                  }
                              }
  
  
                              reportAdapter = new ReportAdapter(appModulesList, POIDocsReportsActivity.this);
                              reportAdapter.notifyDataSetChanged();
  
                              rv.setAdapter(reportAdapter);
  </code></pre>
  
  <p>This is my fetchModulesMenu method: </p>
  
  <pre><code>  public List&lt;ModuleDoi&gt; fetchModuleMenus(final String appId, final String moduleId) {
      moduleDoiList = new ArrayList&lt;&gt;();
      Thread thread = new Thread() {
          Handler handler = new Handler();
          ProgressDialog progressDialog;
          boolean error;
  
          @Override
          public void run() {
  
              handler.post(new Runnable() {
                  @Override
                  public void run() {
                      progressDialog = new ProgressDialog(POIDocsReportsActivity.this, R.style.Theme_AppCompat_DayNight_Dialog_Alert);
                      progressDialog.setMessage("Please wait...");
                      progressDialog.setCancelable(false);
                      progressDialog.show();
                  }
              });
  
              String url = AppConfig.SERVER_URL + "load_module_menus.php";
              HashMap hashMap = new HashMap();
  
              hashMap.put("APPID", appId);
  
              hashMap.put("MODULEID", moduleId);
              JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url, new JSONObject(hashMap), new Response.Listener&lt;JSONObject&gt;() {
                  @Override
                  public void onResponse(JSONObject response) {
                      try {
                          progressDialog.dismiss();
                          JSONObject jsonObject = new JSONObject(response.toString());
                          error = jsonObject.getBoolean("ERROR");
                          if (error){
                              Toast.makeText(POIDocsReportsActivity.this, "MENUS NOT FOUND ", Toast.LENGTH_LONG).show();
                          }
                          else{
                              JSONArray jsonArray = jsonObject.getJSONArray("RESPONSE");
                              for (int i = 0; i &lt; jsonArray.length(); i++){
                                  JSONObject eachMenu = jsonArray.getJSONObject(i);
                                  ModuleDoi moduleDoi = new ModuleDoi(eachMenu.getString("POINAME"), eachMenu.getString("POICODE"));
                                  moduleDoiList.add(moduleDoi);
                              }
                          }
                      } catch (JSONException e) {
                          e.printStackTrace();
                          Toast.makeText(POIDocsReportsActivity.this, "CONNECTION ERROR ", Toast.LENGTH_LONG).show();
                      }
  
                  }
              }, new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      progressDialog.dismiss();
                      Toast.makeText(POIDocsReportsActivity.this, "NO RESPONSE", Toast.LENGTH_LONG).show();
                  }
              });
              AppController.getInstance().addToRequestQueue(jsonObjectRequest);
          }
  
  
      };
      thread.run();
  
     return  moduleDoiList;
  
     }
  </code></pre>
  
  <p>I am using the <a href="https://github.com/thoughtbot/expandable-recycler-view" rel="nofollow noreferrer">Thoughtbot</a> Custom Expandable Recycler View</p>