2

I am implementing ListView with section in which I show custom section headers apart from the conventional alphabets as the header. In order for me to implement the custom SectionIndexer correctly, I wish to understand the difference between the two methods getSectionForPosition and getPositionForSection.

I understand (not sure if that's correct) that getSectionForPosition returns the alphabet we want to show in the section header.

I don't understand the other method. Also, are they similar in any sense (if at all) and in what way they differ (if they do, which I think they do :) )

Anybody having understanding about this may kindly post an answer to this. Appreciate your time going through the question.

Update:

I have gone through the documentation at this official page; I'm looking for some elaborated insight with respect to custom SectionIndex implementation

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114

3 Answers3

5

getPositionForSection(section) returns the first position at which the cursor data at the indexed column starts with the section.

For example if the index of section B is 1 and the indexed column of the cursor has the following data

Position        Data               getSectionForPosition(position)

_________       __________         ______________________________

0               Abhfdf              0

1               Achahtkh            0

2               Ahtjlarej           0

3               Bchatkd             1

4               Bjklhdsfoi          1

5               Bzhafdlsfk          1

6               Cj fadsfkj          2

then getPositionForSection(1) returns 3

also getPositionForSection(2) returns 6

Hope this helps you

Sreejith Krishnan R
  • 1,244
  • 10
  • 12
1

Here's a sample implementation I created. Perhaps it clarifies the SectionIndexer a bit.

For holding your data, you could use private inner classes:

private class Item {
    public int sectionNumber;

    public Long id;
    public String name;
}

private class Section {
    public int startPosition;

    public String header;

    @Override
    public String toString(){
        return header;
    }
}

Use a list for your items and one for your sections:

private List<Item> items = new ArrayList<Item>();
private List<Section> sections = new ArrayList<Section>();

The implementation in your list adapter for SectionIndexer could be like the following now:

@Override
public int getPositionForSection(int sectionNumber) {
    return sections.get(sectionNumber).startPosition;
}

@Override
public int getSectionForPosition(int itemPosition) {
    return items.get(itemPosition).sectionNumber;
}

@Override
public Object[] getSections() {
    return sections.toArray();
}

Ofcourse you should implement a method now to fill your adapter's data lists.

Almer
  • 1,139
  • 1
  • 12
  • 14
0

The method getPositionForSection(int section) returns you the starting index in list for section

while the method getSectionForPosition(int position) returns you the index of section which contains item at position

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
user1049280
  • 5,176
  • 8
  • 35
  • 52