public SectionedListViewAdapter(ListView listView, ListViewDataSource dataSource) { this.listView = listView; this.dataSource = dataSource; mapPosition = new HashMap<Integer, IndexPath>(); if (dataSource != null) { numberOfSections = dataSource.numberOfSectionsInListView(listView); numberSectionHeaders = 0; numberRegularRows = 0; int positionIndex = 0; for (int sectionIndex = 0; sectionIndex < numberOfSections; ++sectionIndex) { int numberRowsInSection = dataSource.listViewNumberOfRowsInSection(listView, sectionIndex); numberRegularRows += numberRowsInSection; String sectionHeader = dataSource.listViewTitleForHeaderInSection(listView, sectionIndex); if (sectionHeader != null) { mapPosition.put(Integer.valueOf(positionIndex), new IndexPath(sectionIndex, -1)); ++numberSectionHeaders; ++positionIndex; } for (int rowIndex = 0; rowIndex < numberRowsInSection; ++rowIndex) { mapPosition.put(Integer.valueOf(positionIndex), new IndexPath(sectionIndex, rowIndex)); ++positionIndex; } } } else { numberOfSections = 0; numberSectionHeaders = 0; numberRegularRows = 0; } numberViews = numberSectionHeaders + numberRegularRows; }
@Override public View getView(int position, View convertView, ViewGroup viewGroup) { IndexPath indexPath = mapPosition.get(Integer.valueOf(position)); if (indexPath.row == -1) { String headerTitle = dataSource.listViewTitleForHeaderInSection(listView, indexPath.section); return headerView(headerTitle); } else { return dataSource.listViewCellForRowAtIndexPath(listView, convertView, indexPath); } }