title: ExpandableListView extended using BaseExpandableListAdapter but reading from Sqlite DB example
body: <p>Senior Geeks.</p>
  
  <p>I'd like to request a simple but fully working example of how to implement an ExpandableListView while extending from BaseExpandableListAdapter Yet Reading data from an Sqlite Database.</p>
  
  <p>I have researched and experimented on the question (see <a href="https://stackoverflow.com/questions/16107985/expandable-listview-with-2-textviews-in-grouprow-xml">here</a>), but with minimal success where i was able to display some data in the header, albeit it was same values repeating for all group headers. Also child items don't show.</p>
  
  <p>The reason for extending with BaseExpandableListAdapter is to have a custom layout for the group header. The reason for SQLite access is naturally because thats where my data is stored.</p>
  
  <p>All examples trawled on the net so far use either SimpleCursorTreeAdapter or CursorTreeAdapter as the extender in DB based applications or simply BaseExpandableListAdapter when data used is in ArrayLists. </p>
  
  <p>Below is the Experimentation thus far. (with this code,only the group header is populated with the same figures over and over. Childitems dont appear)</p>
  
  <pre><code>public class ExpandableListViewAdapterCustom extends BaseExpandableListAdapter {
      protected Activity currentActivity;
      public ExpandableListViewAdapterCustom(Activity callingActivity){
      this.currentActivity = callingActivity;
      }
  
      private Cursor mGroupsCursorLocal ;
      private Cursor mChildCursor;
      private Context ctx;
      private int groupItem;
      private int childItem;
      private String[] fieldsToUseFromGroupCursor;
      private int[] screenTextsToMapGroupDataTo;
      private String[] fieldsToUseFromChildCursor;
      private int[] screenTextsToMapChildDataTo;
  
      public ArrayList&lt;String&gt; tempChild;
      public LayoutInflater minflater;
      public Activity activity;
      public int intGroupTotal;
  
  
      public void setCurrentActivity(Activity activity) {
         this.activity = activity;
      }
  
      public void setCtx(Context ctx) {
         this.ctx = ctx;
      }
  
       public void setGroupItem(int groupItem) {
           this.groupItem = groupItem;
        }
  
      public void setChildItem(int childItem) {
          this.childItem = childItem;
      }
  
      public Activity getCurrentActivity() {
          return currentActivity;
      }
  
       public Cursor getmGroupsCursorLocal() {
          return mGroupsCursorLocal;
        }
  
      public Context getCtx() {
          return currentActivity.getBaseContext();
      }
  
      public void setmGroupsCursorLocal(Cursor mGroupsCursor) {
          this.mGroupsCursorLocal = mGroupsCursor;
      }
  
      public ExpandableListViewAdapterCustom(Cursor mGroupsCursor,
                                         Activity activity,
                                         int groupItem,
                                         int childItem,
                                         String[] fieldsToUseFromGroupCursor,
                                         int[] screenTextsToMapGroupDataTo,
                                         String[] fieldsToUseFromChildCursor,
                                         int[] screenTextsToMapChildDataTo) {
  
  
  
          DatabaseRoutines db = new DatabaseRoutines(activity);
  
          setmGroupsCursorLocal(mGroupsCursor);
          mGroupsCursorLocal = db.fetchGroup();
          activity.startManagingCursor (mGroupsCursor);
          mGroupsCursorLocal.moveToFirst();
  
          mChildCursor=db.fetchChildren(mGroupsCursorLocal.getColumnIndex("Year"));
          mChildCursor.moveToFirst();
          activity.startManagingCursor(mChildCursor);
  
  
  
          setCtx(activity);
          setCurrentActivity(activity);
  
          }
  
          public void setInflater(LayoutInflater mInflater, Activity act) {
          this.minflater = mInflater;
          activity = act;
          }
  
          @Override
          public Object getChild(int groupPosition, int childPosition) {
             return null;
          }
  
          @Override
          public long getChildId(int groupPosition, int childPosition) {
            return 0;
          }
  
  
          @Override
          public View getChildView(int groupPosition, 
              int childPosition,boolean         
              isLastChild, 
              View convertView, 
              ViewGroup parent) {
             View v = convertView;
             if (v == null) 
          {
          LayoutInflater inflater = 
         (LayoutInflater)     ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            
         v = inflater.inflate(R.layout.exp_listview_childrow, parent, false);
          }
  
          TextView txtMonth = (TextView) v.findViewById(R.id.txtMonth);
          TextView txtMonthAmountSent = (TextView)
          v.findViewById(R.id.txtMonthAmountSentValue);
          TextView txtMonthReceived = (TextView)
          v.findViewById(R.id.txtMonthAmountReceivedValue);
          txtMonth.setText(mChildCursor.getString(mChildCursor.getColumnIndex("Month")));
  
      txtMonthAmountSent.setText
     (mChildCursor.getString(mChildCursor.getColumnIndex("TotalSent")));
      txtMonthReceived.setText
  
      (mChildCursor.getString(mChildCursor.getColumnIndex("TotalReceived")));
      return v;
       }
  
  
      @Override
      public int getChildrenCount(int groupPosition) {
          return (mChildCursor.getCount());
         }
  
      @Override
       public Object getGroup(int groupPosition) {
       return null;
      }
  
      @Override
      public int getGroupCount() {
      return mGroupsCursorLocal.getCount();
      }
  
      @Override
      public void onGroupCollapsed(int groupPosition) {
      super.onGroupCollapsed(groupPosition);
      }
  
      @Override
       public void onGroupExpanded(int groupPosition) {
      super.onGroupExpanded(groupPosition);
      }
  
      @Override
      public long getGroupId(int groupPosition) {
      return 0;
      }
  
  
      @Override
      public View getGroupView(
         int groupPosition, 
         boolean isExpanded,
         View convertView,     
         ViewGroup parent) 
       {
           View v = convertView;
           if (v == null) {
          LayoutInflater inflater =  
          (LayoutInflater)  ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = inflater.inflate(R.layout.exp_listview_groupheader, parent, false);
         }
  
         TextView txtYear = (TextView) v.findViewById(R.id.txtYearValue);
         TextView txtAmountSent = (TextView) v.findViewById(R.id.txtAmountSentValue);
         TextView txtAmountRecieved = (TextView) 
         v.findViewById(R.id.txtAmountReceivedValue);
  
         txtYear.setText(mGroupsCursorLocal.getString(
         mGroupsCursorLocal.getColumnIndex("Year")));
  
         txtAmountSent.setText(
         mGroupsCursorLocal.getString(mGroupsCursorLocal.getColumnIndex("TotalSent")));
  
         txtAmountRecieved.setText(
        GroupsCursorLocal.getString(mGroupsCursorLocal.getColumnIndex("TotalReceived")));
        return v;
      }
  
      @Override
      public boolean hasStableIds() {
          return true;
      }
  
      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
      }
  
  
  }
  </code></pre>
  
  <p>The Database code is like this</p>
  
  <pre><code>public Cursor fetchGroup() {
      SQLiteDatabase db = this.getReadableDatabase();  //if memory leaks check here
          String query = "SELECT DISTINCT MIN(ID) AS id, 
      Year, SUM(SentAmount) AS    TotalSent, SUM(ReceivedAmount) AS TotalReceived 
       FROM  MyTbl GROUP BY Year ORDER BY Year DESC ";
        return db.rawQuery(query, null);}
  
  public Cursor fetchChildren(int Yr) {
      SQLiteDatabase db = this.getReadableDatabase(); //if memory leaks check here
      String query = "SELECT  MIN(ID) AS id, 
      Year, Month, SUM(SentAmount) AS TotalSent, 
       SUM(ReceivedAmount) AS TotalReceived        
       FROM  MyTbl Where Year= "+ Yr +"   GROUP BY Year, 
       Month ORDER BY Year DESC, Month DESC";
      return db.rawQuery(query, null);
     } 
  </code></pre>
  
  <p>The Code is called from main activity using the following</p>
  
  <pre><code>    ExpandableListView elv = (ExpandableListView)    
  
      findViewById(R.id.expandableListView);
         ExpandableListAdapter mAdapter = new 
         ExpandableListViewAdapterCustom(mGroupsCursor,    
         MyActivity.this,
              R.layout.exp_listview_groupheader,// Your row layout   for a group
              R.layout.exp_listview_childrow, // Your row layout for a child
              new String[] { "Year",
              "TotalSent",
              "TotalReceived" },// Field(s) to use from group cursor       
              new int[] {R.id.txtYearValue,
                         R.id.txtAmountSentValue,
                         R.id.txtAmountReceivedValue },// Widget ids to put group data                        
                         into new String[] { "Year","Month", 
                         "TotalSent", 
                         "TotalReceived" },  // Field(s) to use from child cursors  new  
                          int[] {R.id.txtMonthValue,
                          R.id.txtMonthAmountSentValue,
                          R.id.txtMonthAmountReceivedValue});// Widget ids to put child d   
                          data into
                          elv.setClickable(true);
                          elv.setAdapter(mAdapter); // set the
  </code></pre>