title: How to Click on group if child is empty in expandable listview android
body: <p>I have a problem in Expandable list view. Actually, the details for group and child are coming from server. There are some group that not contains any child element. so how we make that group Clickable and when on click move to an activity. It works fine when there is children for group and when on click on that child, it moves to an activity. Problem occurs when there is no child element in group.
  Any help is deeply appreciated. Thanks in advance ... This is the activity class</p>
  
  <pre><code>public class NewsTabActivity extends Activity {
  
  public static String Id, LOGO, WEB_SERV, SUB_CAT;
  ImageView image;
  ImageLoader imgloader;
  String URL_MAIN_CAT, nDrawerSubCat, URL_SUB_CAT, URL_HOME_PAGE,
          URL_MAIN_CAT_ONLY;
  String[] mainCatName, mainCatId, subCatParentID;
  JSONArray jarray, jarray1;
  JSONObject jobject, jobject1;
  
  private DrawerLayout mDrawerLayout;
  private ListView homeListview, headerListView;
  private ExpandableListView mDrawerList;
  private ActionBarDrawerToggle mDrawerToggle;
  
  // nav drawer title
  private CharSequence mDrawerTitle;
  // used to store app title
  private CharSequence mTitle;
  
  // slide menu items
  private String[] navMenuTitles;
  
  ArrayList&lt;NewsList&gt; newsList;
  private ArrayList&lt;NewsDrawerItem&gt; navDrawerItems;
  private NewsDrawerItem newsDrawerItems;
  
  private ArrayList&lt;NewsDrawerItem&gt; listNewsHeader = null;
  private ArrayList&lt;NewsDrawerChildItem&gt; listNewsChild = null;
  private ArrayList&lt;ArrayList&lt;NewsDrawerChildItem&gt;&gt; listChildXXXXXXXXX = null;
  
  NewsListAdapter adapter1;
  private NewsDrawerListAdapter adapter;
  private NewsDrawerHeaderAdapter adapterGroup;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_hometab);
  
      mTitle = mDrawerTitle = getTitle();
  
      image = (ImageView) findViewById(R.id.imageView1);
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      mDrawerList = (ExpandableListView) findViewById(R.id.list_slidermenu);
      homeListview = (ListView) findViewById(R.id.listView1);
      headerListView = (ListView) findViewById(R.id.list_header);
  
      // homeListview.setVisibility(View.VISIBLE);
      // mDrawerList.setVisibility(View.VISIBLE);
  
      imgloader = new ImageLoader(NewsTabActivity.this);
      imgloader.DisplayImage(LOGO, image);
      nDrawerSubCat = SUB_CAT;
  
      navDrawerItems = new ArrayList&lt;NewsDrawerItem&gt;();
      newsList = new ArrayList&lt;NewsList&gt;();
      listNewsHeader = new ArrayList&lt;NewsDrawerItem&gt;();
      listNewsChild = new ArrayList&lt;NewsDrawerChildItem&gt;();// ArrayList&lt;ArrayList&lt;ChildTerbaru&gt;&gt;&gt;();
      listChildXXXXXXXXX = new ArrayList&lt;ArrayList&lt;NewsDrawerChildItem&gt;&gt;();
  
      System.out.println("DRAWER_SUBCAT ? = " + nDrawerSubCat);
      URL_HOME_PAGE = WEB_SERV + "homenews.php";
      URL_MAIN_CAT = WEB_SERV + "maincategory.php";
      URL_MAIN_CAT_ONLY = WEB_SERV + "maincategory.php";
  
      if (nDrawerSubCat.equalsIgnoreCase("yes")) {
          URL_SUB_CAT = WEB_SERV + "subcategory.php";
          System.out.println("DRAWER_SUBCAT = " + URL_SUB_CAT);
      }
  
      if (nDrawerSubCat.equalsIgnoreCase("yes")) {
          mDrawerList.setOnChildClickListener(new SlideMenuClickListener());
      } else {
          headerListView
                  .setOnItemClickListener(new SlideMenuClickHeaderListener());
      }
  
      if (WebserviceUtils.isConnectingToInternet(NewsTabActivity.this))   {
          new NewsHomePage().execute(URL_HOME_PAGE);
          if (nDrawerSubCat.equalsIgnoreCase("yes")) {
              new NewsPaperSubCat().execute();
          } else {
              new NewsPaperMainCat().execute(URL_MAIN_CAT_ONLY);
  
              // mDrawerList.setVisibility(View.GONE);
              // headerListView.setVisibility(View.VISIBLE);
              // adapterGroup = new
              // NewsDrawerHeaderAdapter(getApplicationContext(),
              // navDrawerItems);
              // headerListView.setAdapter(adapterGroup);
          }
      }
  
      // setting the nav drawer list adapter
  
      // enabling action bar app icon and behaving it as toggle button
      getActionBar().setDisplayHomeAsUpEnabled(true);
  
      mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
              R.drawable.ic_drawer, // nav menu toggle icon
              R.string.app_name, // nav drawer open - description for
                                  // accessibility
              R.string.app_name // nav drawer close - description for
                                  // accessibility
      ) {
          public void onDrawerClosed(View view) {
              getActionBar().setTitle("News Live");
              // calling onPrepareOptionsMenu() to show action bar icons
              invalidateOptionsMenu();
          }
  
          public void onDrawerOpened(View drawerView) {
              getActionBar().setTitle("News Live");
              // calling onPrepareOptionsMenu() to hide action bar icons
              invalidateOptionsMenu();
          }
      };
      mDrawerLayout.setDrawerListener(mDrawerToggle);
  
      if (savedInstanceState == null) {
          // on first time display view for first nav item
          displayView();
      }
  
  }
  
  /**
   * Slide menu item click listener
   * */
  private class SlideMenuClickHeaderListener implements
          ListView.OnItemClickListener {
      @Override
      public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
              long id) {
          // display view for selected nav drawer item
          displayView();
          mDrawerList.setVisibility(View.GONE);
          headerListView.setVisibility(View.VISIBLE);
          NewsListActivity.valuesID(navDrawerItems.get(position).getTitle());
          NewsListActivity.valueWEBSERV(WEB_SERV);
          System.out.println("NEWS_LIST_ID = "
                  + navDrawerItems.get(position).getTitle());
      }
  }
  
  private class SlideMenuClickListener implements
          ExpandableListView.OnChildClickListener,
          ExpandableListView.OnGroupClickListener {
  
      @Override
      public boolean onChildClick(ExpandableListView parent, View view,
              int groupPosition, int childPosition, long id) {
          // TODO Auto-generated method stub
          // if (nDrawerSubCat.equalsIgnoreCase("no")) {
          // displayView();
          // homeListview.setVisibility(View.GONE);
          // NewsListActivity.valuesID(navDrawerItems.get(groupPosition)
          // .getId());
          // NewsListActivity.valueWEBSERV(WEB_SERV);
          // System.out.println("NEWS_EXP_GROUP_LIST_ID = "
          // + navDrawerItems.get(groupPosition).getId());
          // } else {
          displayView();
          homeListview.setVisibility(View.GONE);
          NewsListActivity.valuesID(listNewsChild.get(childPosition).getId());
          NewsListActivity.valueWEBSERV(WEB_SERV);
          System.out.println("NEWS_EXP_CHILD_LIST_ID = "
                  + listNewsChild.get(childPosition).getId());
          // }
          return false;
      }
  
      @Override
      public boolean onGroupClick(ExpandableListView parent, View v,
              int groupPosition, long id) {
          // TODO Auto-generated method stub
          displayView();
          // homeListview.setVisibility(View.GONE);
          NewsListActivity
                  .valuesID(navDrawerItems.get(groupPosition).getId());
          NewsListActivity.valueWEBSERV(WEB_SERV);
          System.out.println("NEWS_EXP_GROUP_LIST_ID = "
                  + navDrawerItems.get(groupPosition).getId());
          return false;
      }
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // toggle nav drawer on selecting action bar app icon/title
      if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
      }
      // Handle action bar actions click
      return super.onOptionsItemSelected(item);
  }
  
  /* *
   * Called when invalidateOptionsMenu() is triggered
   */
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
      // if nav drawer is opened, hide the action items
      return super.onPrepareOptionsMenu(menu);
  }
  
  /**
   * Diplaying fragment view for selected nav drawer list item
   * */
  private void displayView() {
      // update the main content by replacing fragments
      Fragment fragment = null;
      fragment = new NewsListActivity();
  
      if (fragment != null) {
          FragmentManager fragmentManager = getFragmentManager();
          fragmentManager.beginTransaction()
                  .replace(R.id.frame_container, fragment).commit();
  
          if (nDrawerSubCat.equalsIgnoreCase("yes")) {
              mDrawerLayout.closeDrawer(mDrawerList);
          } else {
              mDrawerLayout.closeDrawer(headerListView);
          }
      } else {
          // error in creating fragment
          Log.e("NewsTabActivity", "Error in creating fragment");
      }
  }
  
  private class NewsHomePage extends AsyncTask&lt;String, String, String&gt; {
  
      private ProgressDialog p_dialog;
  
      @Override
      protected void onPreExecute() {
          // TODO Auto-generated method stub
          p_dialog = ProgressDialog.show(NewsTabActivity.this, "",
                  "Loading...Please wait...", true);
          p_dialog.setCancelable(true);
          p_dialog.show();
          super.onPreExecute();
      }
  
      @Override
      protected String doInBackground(String... arg0) {
          // TODO Auto-generated method stub
          JsonParsor json = new JsonParsor();
          JSONObject jobject = json.getJSONFromUrl(URL_HOME_PAGE);
  
          try {
              JSONArray jarray = jobject.getJSONArray("news");
  
              for (int i = 0; i &lt; jarray.length(); i++) {
                  JSONObject jobj = jarray.getJSONObject(i);
                  String id = jobj.getString("Id");
                  String cid = jobj.getString("c_id");
                  String title = jobj.getString("title");
                  String image = jobj.getString("image");
  
                  NewsList news = new NewsList();
                  news.setId(jobj.getString("Id"));
                  news.setCid(jobj.getString("c_id"));
                  news.setTitle(jobj.getString("title"));
                  news.setImage(jobj.getString("image"));
                  newsList.add(news);
              }
          } catch (Exception e) {
              // TODO: handle exception
              e.printStackTrace();
          }
          return "sucess";
      }
  
      @Override
      protected void onPostExecute(String result) {
          // TODO Auto-generated method stub
          p_dialog.dismiss();
          if (result.compareTo("sucess") == 0) {
              navDrawerItems.clear();
              adapter1 = new NewsListAdapter(NewsTabActivity.this,
                      R.layout.newslist_single_item, newsList);
              homeListview.setAdapter(adapter1);
              adapter1.notifyDataSetChanged();
          } else {
              Toast.makeText(NewsTabActivity.this, "No data Found...",
                      Toast.LENGTH_LONG).show();
          }
      }
  
  }
  
     private class NewsPaperSubCat extends AsyncTask&lt;String, String, String&gt; {
  
      private ProgressDialog p_dialog;
  
      @Override
      protected void onPreExecute() {
          // TODO Auto-generated method stub
          p_dialog = ProgressDialog.show(NewsTabActivity.this, "",
                  "Loading...Please wait...", true);
          p_dialog.setCancelable(true);
          p_dialog.show();
          super.onPreExecute();
      }
  
      @Override
      protected String doInBackground(String... arg0) {
          // TODO Auto-generated method stub
  
          JsonParsor json = new JsonParsor();
          jobject = json.getJSONFromUrl(URL_MAIN_CAT);
          // if (nDrawerSubCat.equalsIgnoreCase("yes")) {
          jobject1 = json.getJSONFromUrl(URL_SUB_CAT);
          // }
  
          try {
              if (nDrawerSubCat.equalsIgnoreCase("yes")) {
                  NewsDrawerItem LT = null;
                  NewsDrawerChildItem CT = null;
  
                  listNewsChild = new ArrayList&lt;NewsDrawerChildItem&gt;();
  
                  jarray = jobject.getJSONArray("categories");
                  // if (nDrawerSubCat.equalsIgnoreCase("yes")) {
                  jarray1 = jobject1.getJSONArray("categories");
                  // }
  
                  for (int i = 0; i &lt; jarray.length(); i++) {
                      JSONObject jobj = jarray.getJSONObject(i);
  
                      mainCatName = new String[jarray.length()];
                      mainCatId = new String[jarray.length()];
  
                      mainCatId[i] = jobj.getString("Id");
                      mainCatName[i] = jobj.getString("name");
                      System.out.println("mainCatId[" + i + "] = "
                              + mainCatId[i]);
  
                      String id = jobj.getString("Id");
                      String name = jobj.getString("name");
  
                      System.out
                              .println("MAIN_ID = " + id + " NAME= " + name);
  
                      // adding nav drawer items to array
                      LT = new NewsDrawerItem(jobj.getString("Id"),
                              jobj.getString("name"));
                      listNewsHeader.add(LT);
  
                      // int listsize = listNewsHeader.size();
                      // for(int k=0 ; k&lt;listsize; k++) {
                      // System.out.println("HEADER_VALUE["+k+"] = "+listNewsHeader.get(k));
                      // }
                  }
  
                  // if (nDrawerSubCat.equalsIgnoreCase("yes")) {
                  for (int i = 0; i &lt; jarray1.length(); i++) {
                      JSONObject jobj = jarray1.getJSONObject(i);
  
                      subCatParentID = new String[jarray1.length()];
                      subCatParentID[i] = jobj.getString("parent_id");
                      System.out.println("mainCatId_SubCatId[" + i + " = "
                              + subCatParentID[i]);
  
                      CT = new NewsDrawerChildItem(jobj.getString("Id"),
                              jobj.getString("name"));
  
                      System.out.println("SUB_MAIN_ID = "
                              + jobj.getString("Id") + " NAME= "
                              + jobj.getString("name"));
  
                      // if(mainCatId[i].contains(subCatParentID[i])) {
                      // String[] temp = new String[mainCatId.length];
                      // temp[i] = jobj.getString("Id");
                      // System.out.println("mainCatId_Temp = "+ temp[i]);
                      // }
                      List similarities = new ArrayList();
                      for (int j = 0; j &lt; Math.max(mainCatId.length,
                              subCatParentID.length); j++) {
                          if (mainCatId[i] == subCatParentID[i])
                              similarities.add(mainCatId[i]);
                          System.out.println("VALU = " + similarities);
                      }
  
                      listNewsChild.add(CT);
                      listChildXXXXXXXXX.add(listNewsChild);
                  }
                  // }
              }
              // else {
              // jarray = jobject.getJSONArray("categories");
              //
              // for (int i = 0; i &lt; jarray.length(); i++) {
              // JSONObject jobj = jarray.getJSONObject(i);
              //
              // newsDrawerItems = new NewsDrawerItem();
              //
              // mainCatName = new String[jarray.length()];
              // mainCatId = new String[jarray.length()];
              //
              // mainCatId[i] = jobj.getString("Id");
              // mainCatName[i] = jobj.getString("name");
              //
              // String id = jobj.getString("Id");
              // String name = jobj.getString("name");
              //
              // System.out.println("MAIN_ID = " + id + " NAME= " + name);
              //
              // // adding nav drawer items to array
              // // navDrawerItems.add(new NewsDrawerItem(mainCatName[i],
              // mainCatId[i]));
              // }
              // }
          } catch (Exception e) {
              // TODO: handle exception
              e.printStackTrace();
          }
          return "sucess";
      }
  
      @Override
      protected void onPostExecute(String result) {
          // TODO Auto-generated method stub
          p_dialog.dismiss();
          super.onPostExecute(result);
  
          if (result.compareTo("sucess") == 0) {
              if (nDrawerSubCat.equalsIgnoreCase("yes")) {
                  mDrawerList.setVisibility(View.GONE);
                  adapter = new NewsDrawerListAdapter(NewsTabActivity.this,
                          listNewsHeader, listChildXXXXXXXXX);
                  mDrawerList.setAdapter(adapter);
                  adapter.notifyDataSetChanged();
              }
          } else {
              Toast.makeText(NewsTabActivity.this, "No data Found...",
                      Toast.LENGTH_LONG).show();
          }
      }
  }
  
  private class NewsPaperMainCat extends AsyncTask&lt;String, String, String&gt; {
  
      private ProgressDialog p_dialog;
  
      @Override
      protected void onPreExecute() {
          // TODO Auto-generated method stub
          p_dialog = ProgressDialog.show(NewsTabActivity.this, "",
                  "Loading...Please wait...", true);
          p_dialog.setCancelable(true);
          p_dialog.show();
          super.onPreExecute();
      }
  
      @Override
      protected String doInBackground(String... arg0) {
          // TODO Auto-generated method stub
          JsonParsor json1 = new JsonParsor();
          JSONObject jObject = json1.getJSONFromUrl(URL_MAIN_CAT_ONLY);
  
          try {
              NewsDrawerItem NDI = null;
  
              JSONArray jarr = jObject.getJSONArray("categories");
  
              for (int i = 0; i &lt; jarr.length(); i++) {
                  JSONObject jObj = jarr.getJSONObject(i);
  
                  mainCatName = new String[jarr.length()];
                  mainCatId = new String[jarr.length()];
  
                  mainCatId[i] = jObj.getString("Id");
                  mainCatName[i] = jObj.getString("name");
  
                  String id = jObj.getString("Id");
                  String name = jObj.getString("name");
  
                  System.out.println("MAIN_ID_ONLY = " + id
                          + " MAIN_NAME_ONLY= " + name);
  
                  // adding nav drawer items to array
                  navDrawerItems.add(new NewsDrawerItem(mainCatName[i],
                          mainCatId[i]));
              }
          } catch (Exception e) {
              // TODO: handle exception
          }
          return "sucess";
      }
  
      @Override
      protected void onPostExecute(String result) {
          // TODO Auto-generated method stub
          p_dialog.dismiss();
          super.onPostExecute(result);
  
          if (result.compareTo("sucess") == 0) {
              mDrawerList.setVisibility(View.GONE);
              headerListView.setVisibility(View.VISIBLE);
              adapterGroup = new NewsDrawerHeaderAdapter(
                      NewsTabActivity.this, navDrawerItems);
              headerListView.setAdapter(adapterGroup);
              adapterGroup.notifyDataSetChanged();
          } else
              Toast.makeText(NewsTabActivity.this, "No data Found...",
                      Toast.LENGTH_LONG).show();
      }
  }
  
  public static void valueID(String id) {
      // TODO Auto-generated method stub
      Id = id;
  }
  
  public static void valueLogo(String logo) {
      // TODO Auto-generated method stub
      LOGO = logo;
  }
  
  public static void valueWebService(String webServ) {
      // TODO Auto-generated method stub
      WEB_SERV = webServ;
  }
  
  public static void valueSubCat(String subCat) {
      // TODO Auto-generated method stub
      SUB_CAT = subCat;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the Expandable adapter class,</p>
  
  <pre><code> public class NewsDrawerHeaderAdapter extends BaseAdapter {
  
  private Context context;
  private ArrayList&lt;NewsDrawerItem&gt; navDrawerItems;
  
  public NewsDrawerHeaderAdapter(Context context,
          ArrayList&lt;NewsDrawerItem&gt; navDrawerItems) {
      this.context = context;
      this.navDrawerItems = navDrawerItems;
  }
  
  @Override
  public int getCount() {
      // TODO Auto-generated method stub
      return navDrawerItems.size();
  }
  
  @Override
  public Object getItem(int position) {
      // TODO Auto-generated method stub
      return navDrawerItems.get(position);
  }
  
  @Override
  public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      if (convertView == null) {
          LayoutInflater mInflater = (LayoutInflater) context
                  .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
          convertView = mInflater.inflate(R.layout.drawer_list_item, null);
      }
      TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
  
      txtTitle.setText(Html.fromHtml(navDrawerItems.get(position).getId()));
      return convertView;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and here is the model class</p>
  
  <pre><code>public class NewsDrawerItem {
  
  private String title, Id, PId;
  private int icon;
  
  private ArrayList&lt;NewsDrawerItem&gt; childItemList = new ArrayList&lt;NewsDrawerItem&gt;();
  
  public NewsDrawerItem() {
  }
  
  public NewsDrawerItem(String Id, String title) {
      this.Id = Id;
      this.title = title;
  }
  
  public NewsDrawerItem(String Id, String title, String PId) {
      this.Id = Id;
      this.title = title;
      this.PId = PId;
  }
  
  public String getTitle() {
      return this.title;
  }
  
  public String getId() {
      return this.Id;
  }
  
  public String getPId() {
      return this.PId;
  }
  
  
  public int getIcon() {
      return this.icon;
  }
  
  public ArrayList&lt;NewsDrawerItem&gt; getChildItem() {
      System.out.println("CHILD_ITEM = "+childItemList);
      return childItemList;
  }
  
  public void setTitle(String title) {
      this.title = title;
  }
  
  public void setId(String Id) {
      this.Id = Id;
  }
  
  public void setPId(String PId) {
      this.PId = PId;
  }
  
  public void setIcon(int icon) {
      this.icon = icon;
  }
  
  public void setChildItem(ArrayList&lt;NewsDrawerItem&gt; childItemList) {
      this.childItemList = childItemList;
  }
  </code></pre>
  
  <p>}</p>