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 8465681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:07:12+00:00 2026-06-10T15:07:12+00:00

I have created an alphabetical adapter, inspiring from this post with the adding of

  • 0

I have created an alphabetical adapter, inspiring from this post with the adding of headers and for an expandableListView.
Each group of my list have exactly one child.

Sometimes, and for reason that I don’t understand, Android give me an IndexOutOfBoundsException with the getPositionForSection method. The section called is out of bound of the my sections string array. I think it’s because of the expandable list but I’m not sure. Can you help me ?

public class AlphabeticalAdapter extends BaseExpandableListAdapter implements SectionIndexer{

private HashMap<String, Integer> alphaIndexer;
private String[] sections;
private HashMap<String, String> dictionnary;
private ArrayList<String> words;
private LayoutInflater inflater;

public AlphabeticalAdapter(Context context, HashMap<String, String> dictionnary){

    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.dictionnary = dictionnary;
    this.words = new ArrayList<String>(this.dictionnary.keySet());
    Collections.sort(this.words, String.CASE_INSENSITIVE_ORDER);

    this.alphaIndexer = new HashMap<String, Integer>();
    for (int i = 0; i < this.words.size(); i++){
        String s = this.words.get(i).substring(0, 1).toUpperCase();
        if (!this.alphaIndexer.containsKey(s)){
            this.alphaIndexer.put(s, i);
        }
    }

    Set<String> sectionLetters = this.alphaIndexer.keySet();
    ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
    Collections.sort(sectionList);

    this.sections = new String[sectionList.size()];
    for (int i = 0; i < sectionList.size(); i++){
        this.sections[i] = sectionList.get(i);
    }

}

@Override
public int getSectionForPosition(int position){

    String currentFirstChar = this.words.get(position).substring(0, 1).toUpperCase();
    for (int section = 0; section < this.sections.length; section++){
        if (this.sections[section].equals(currentFirstChar)){
            return section;
        }
    }

    return 1;

}

@Override
public int getPositionForSection(int section){
    return this.alphaIndexer.get(this.sections[section]);   
}

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

@Override
public Object getGroup(int groupPosition) {
    return this.words.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.dictionnary.get(getGroup(groupPosition));
}

@Override
public int getGroupCount() {
    return this.dictionnary.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    View view = (View) inflater.inflate(R.layout.glossary_word_row, null);

    TextView header = (TextView) view.findViewById(R.id.section);
    TextView rowText = (TextView) view.findViewById(R.id.textView);

    String currentWord = this.words.get(groupPosition);
    String currentFirstChar = currentWord.substring(0, 1).toUpperCase();

    rowText.setText(currentWord);

    if (groupPosition == this.alphaIndexer.get(currentFirstChar)){
        header.setVisibility(View.VISIBLE);
        header.setText(currentFirstChar);
    }

    return view;

}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    View view = (View) this.inflater.inflate(R.layout.glossary_word_row, null);
    TextView rowText = (TextView) view.findViewById(R.id.textView);
    rowText.setText(this.dictionnary.get(this.words.get(groupPosition)));

    return view;

}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return false;
}

}
  • 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-10T15:07:13+00:00Added an answer on June 10, 2026 at 3:07 pm

    I don’t understand why this method is called with wrong parameter, but I have seen in the official Android documentation this :

    Returns : the starting position of that section. If the section is out of bounds, the position must be clipped to fall within the size of the list.

    So I have write this :

    @Override
    public int getPositionForSection(int section){
        if (section >= this.sections.length){
            return getGroupCount() - 1;
        }
        return this.alphaIndexer.get(this.sections[section]);   
    }
    

    And all is working well. Problem solved 🙂

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

Sidebar

Related Questions

I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created a custom post type named People. I have created a page
I have a folder with backups from a MySQL database that are created automatically.
I have a list of countries in a database. I have created a select
I have created a android list view in Eclipse that I would like to
Is the xml that is created from your DataContract created in alphabetical order. I
I have created a QWidget with a bunch of QToolButtons in it and I
I have created some JQuery that will expand a div 'popup' on hover and
I have created a JSP / servlets application running in Tomcat 7. It runs
I have created an EDMX in visual studio 2010 SP1. It has been built

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.