Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8547077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:11:17+00:00 2026-06-11T13:11:17+00:00

Explain me the usage of getPositionForSection(int) and getSectionForPosition(int) and getSections() methods of ArrayAdapter ?

  • 0

Explain me the usage of getPositionForSection(int) and getSectionForPosition(int) and getSections() methods of ArrayAdapter?

I got a good explanation of getViewTypeCount and getItemViewType methods of ArrayAdapter.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:11:18+00:00Added an answer on June 11, 2026 at 1:11 pm
    Charsequence alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    

    Here

    A = 0
    B = 1
    C = 2
    D = 3
    E = 4
    F = 5
    .
    .
    .
    Z = 25
    

    Create a new AlphabetIndexer

    AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, alphabets);
    

    getSections() retreives the String[] of the alphabets that is provided to the alphabetIndexer

    String[] sections = (String[]) alphabetIndexer.getSections();
    

    alphabetIndexer.getSectionForPosition(position) retreives the section for the current position

    Consider the given case

    position  Data             getSectionForPosition(position)
    ________  _________            __________
    
       0      Ajdhfj j             0
       1      Aadf hdsf            0
       2      Ajfkldsahf           0
       3      Asdhfa df            0
       4      Badhf                1
       5      Bdfj sadif           1
       6      Bghoi ij             1
       7      Bjkojg o             1
       8      Cadj fkljsdf         2
       9      Cgjds kfja           2
       10     Cn khdfaj            2
       11     Cph iohsdf           2
       12     Czjfa sh             2
       13     Dfgoa hjoifaj        3
       14     Dzfjdak sfh          3
       15     Fhf adhf             5
       16     Zdfh ajdf            25
    

    To get the section for the position

    String section = sections[getSectionForPosition(position)];
    

    alphabetIndexer.getPositionForSection(section) retreives the first position at which the data starts with that section

    section      getPositionForSection(section)
    _________    ____________________
    0            0
    1            4
    2            8
    3            13
    4            13
    5            15
    6            15
    7            15
    8            15
    .            .
    .            .
    .            .
    23           15
    24           15          
    25           16
    

    Hope this helps you.

    An example of using alphabetIndexer

    public abstract class SectionedListAdapter extends ResourceCursorAdapter {
    
    private SparseIntArray sections;
    private SparseIntArray cursorPositions;
    private Context mContext;
    private int sortedColumnIndex;
    
    private static final int NORMAL_LIST_VIEW = 0;
    private static final int SECTION_LIST_VIEW = 1;
    private static final int NULL_LIST_VIEW = 2;
    
    public SectionedListAdapter(Context context, int layout, Cursor c,
                                boolean autoRequery, int sortedColumnIndex, int defaultBitmapResId) {
        super(context, layout, c, autoRequery);
        this.mContext = context;
        this.sortedColumnIndex = sortedColumnIndex;
        setSortedCursorColumn(c);
    }
    
    public void setSortedCursorColumn(Cursor cursor){
        if (cursor == null){
            return;
        }
        AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        sections = new SparseIntArray();
        int m=0;
        int t = 'A';
        for (int i=0; i < 26; i++){
            if ((i+1) < 26) {
                int position = alphabetIndexer.getPositionForSection(i);
                int temp = alphabetIndexer.getPositionForSection(i + 1);
                if (temp != position){
                    sections.put(position + m, t+i);
                    m++;
                }
            } else {
                int position = alphabetIndexer.getPositionForSection(i);
                int temp = alphabetIndexer.getPositionForSection(i-1);
                if (position != temp){
                    sections.put(position + m, t+i);
                }
            }
        }
        int temp = 0;
        cursorPositions = new SparseIntArray();
        for (int i=0; i<cursor.getCount() + sections.size(); i++){
            if (sections.get(i, -1) != -1){
                temp ++;
                cursorPositions.put(i, -1);
            } else {
                cursorPositions.put(i, i - temp);
            }
        }
    }
    
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (getItemViewType(position) == NORMAL_LIST_VIEW){
            if (view == null){
                view = newView(mContext, getCursor(), parent);
            }
            getCursor().moveToPosition(cursorPositions.get(position));
            bindView(view, mContext, getCursor(), position);
        } else if (getItemViewType(position) == SECTION_LIST_VIEW) {
            if (view == null){
                view = newSectionView(mContext, getCursor(), parent);
            }
            bindSectionView(view, mContext, (char)sections.get(position));
        } else {
            if (view == null){
            } else {
                view = new ViewStub(mContext);
            }
        }
        return view;
    }
    
    public abstract View newSectionView(Context context, Cursor cursor, ViewGroup parent);
    
    public abstract void bindSectionView(View view, Context context, char text);
    
    public abstract void bindView(View view, Context context, Cursor cursor, int position);
    
    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }
    
    @Override
    public boolean isEnabled(int position) {
        return cursorPositions.get(position) >= 0;
    }
    
    @Override
    public int getItemViewType(int position) {
        if (cursorPositions.get(position) >= 0){
            return NORMAL_LIST_VIEW;
        }else if (cursorPositions.get(position) == -1){
            return SECTION_LIST_VIEW;
        } else {
            return NULL_LIST_VIEW;
        }
    }
    
    @Override
    public int getViewTypeCount() {
        return 3;
    }
    
    private void putCursorExclusion(int position, boolean isActualPosition){
        if (isActualPosition) {
            cursorPositions.put(position, -cursorPositions.get(position));
        } else {
            int actualPosition = cursorPositions.keyAt(cursorPositions.indexOfValue(position));
            cursorPositions.put(actualPosition, -position);
        }
    }
    
    private void removeCursorExclusion(int position, boolean isActualPosition){
        if (isActualPosition){
            cursorPositions.put(position, -cursorPositions.get(position));
        } else {
            int actualPosition = cursorPositions.keyAt(cursorPositions.indexOfValue(-position));
            cursorPositions.put(actualPosition, position);
        }
    }
    
    @Override
    public int getCount() {
        return cursorPositions == null ? 0 : cursorPositions.size();
    }
    
    public SparseIntArray getSections() {
        return sections;
    }
    
    public SparseIntArray getCursorPositions() {
        return cursorPositions;
    }
    
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        //do nothing here
    }
    
    @Override
    public Cursor swapCursor(Cursor newCursor) {
        return super.swapCursor(newCursor);
    }
    
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }
    
    @Override
    public void changeCursor(Cursor cursor) {
        setSortedCursorColumn(cursor);
        super.changeCursor(cursor);
    }
    }
    

    To use this just create a new class extends SectionedListAdapter and then provide required details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

const int* const Method3(const int* const&) const; Can someone explain the usage of each
Can anyone explain usage of weak references? The documentation doesn't explain it precisely, it
Can any one explain the difference between and usage of OR operator ( ||
I can't understand the usage of glOrtho . Can someone explain what it is
Please explain what is meant by tuples in sql?Thanks..
please explain purpose of usage of locale in c++? i have read documents but
Can someone explain to me the usage of Integer, Boolean etc in place of
Can anybody explain why this tiny app's memory usage keeps increasing ? static class
I'm trying to find a good metaphor to explain memory allocation, initialization and freeing
Can any one explain actual usage of run script in xcode 4. what is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.