2012-08-26 43 views
0

我已經創建了一個字母適配器,從this post鼓舞並添加了頭文件和一個expandableListView。 我的列表中的每一組都有一個孩子。BaseExpandListList中的IndexOutOfBoundsException實現SectionIndexer

有時,由於我不明白,Android給我一個IndexOutOfBoundsException與getPositionForSection方法。被調用的部分超出了我的部分字符串數組的範圍。我認爲這是因爲可擴展名單,但我不確定。你可以幫我嗎 ?

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; 
} 

} 

回答

4

我不明白爲什麼這個方法被調用參數不正確,但我已經在這個the official Android documentation看到:

返回:段的起始位置。如果該部分超出範圍,則該位置必須被裁剪以符合列表的大小。

所以我必須這樣寫:

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

,一切都運作良好。問題解決:-)

+0

謝謝Sebastien!它的工作原理! –

相關問題