SectionAndItem<T> getSectionAndItem(int position) { if (sectionKeys.size() == 0) { return null; } String sectionKey = null; T graphObject = null; if (!displaySections) { sectionKey = sectionKeys.get(0); List<T> section = graphObjectsBySection.get(sectionKey); if (position >= 0 && position < section.size()) { graphObject = graphObjectsBySection.get(sectionKey).get(position); } else { // We are off the end; we must be adding an activity circle to indicate more data is coming. assert dataNeededListener != null && cursor.areMoreObjectsAvailable(); // We return null for both to indicate this. return new SectionAndItem<T>(null, null); } } else { // Count through the sections; the "0" position in each section is the header. We decrement // position each time we skip forward a certain number of elements, including the header. for (String key : sectionKeys) { // Decrement if we skip over the header if (position-- == 0) { sectionKey = key; break; } List<T> section = graphObjectsBySection.get(key); if (position < section.size()) { // The position is somewhere in this section. Get the corresponding graph object. sectionKey = key; graphObject = section.get(position); break; } // Decrement by as many items as we skipped over position -= section.size(); } } if (sectionKey != null) { // Note: graphObject will be null if this represents a section header. return new SectionAndItem<T>(sectionKey, graphObject); } else { throw new IndexOutOfBoundsException("position"); } }
@Override public int getPositionForSection(int section) { if (displaySections) { section = Math.max(0, Math.min(section, sectionKeys.size() - 1)); if (section < sectionKeys.size()) { return getPosition(sectionKeys.get(section), null); } } return 0; }