我已使用ListView
实施了SectionIndexer
。它有99%的时间都在工作,但我发现了一个出错的情况。
错误是:
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
正在发生在这段代码中:
@Override
public int getPositionForSection(int section) {
return indexer.get(sections[section]);
}
调试我发现上面的方法,对于我在本文末尾提供的输入,被调用了两次。第二次,当section
假定值1
时,它会给出错误。
我的适配器以这种方式实现:
public class SearchAdapter extends ArrayAdapter<String[]> implements SectionIndexer {
private final Context context;
private ArrayList<String[]> foodData;
private HashMap<String, Integer> indexer;
private String[] sections;
public SearchAdapter(Context context, ArrayList<String[]> allFoodData) {
super(context, R.layout.rowlist, allFoodData);
this.context = context;
this.foodData = allFoodData;
indexer = new HashMap<String, Integer>();
int size = foodData.size();
for (int x = 0; x < size; x++) {
String[] s = foodData.get(x);
System.out.println("x = " + x + ": foodata[0]=" + s[0] + ": foodata[1]=" + s[1] + ": foodata[2]=" + s[2] + ": foodata[3]=" + s[3]);
String ch = s[1].substring(0,1);
ch = ch.toUpperCase();
if (!indexer.containsKey(ch))
indexer.put(ch, x);
}
Set<String> sectionLetters = indexer.keySet();
// create a list from the set to sort
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
sections = sectionList.toArray(sections);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
\\\\\\\ stuff
}
// ##################### LISTVIEW INDEX #####################
@Override
public Object[] getSections() {
return sections;
}
@Override
public int getPositionForSection(int section) {
return indexer.get(sections[section]);
}
@Override
public int getSectionForPosition(int position) {
return 0;
}
}
输入是:
x = 0: foodata[0]=81: foodata[1]=Iogurte Açucarado batido gordo com cereais e fruta: foodata[2]=PT INSA: foodata[3]=1
x = 1: foodata[0]=80: foodata[1]=Iogurte Açucarado batido gordo com fruta: foodata[2]=PT INSA: foodata[3]=1
x = 2: foodata[0]=70: foodata[1]=Iogurte Açucarado batido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 3: foodata[0]=75: foodata[1]=Iogurte Açucarado batido meio gordo com fruta: foodata[2]=PT INSA: foodata[3]=1
x = 4: foodata[0]=69: foodata[1]=Iogurte Açucarado com edulcorante de síntese, batido magro com cereais: foodata[2]=PT INSA: foodata[3]=1
x = 5: foodata[0]=68: foodata[1]=Iogurte Açucarado com edulcorante de síntese, sólido magro: foodata[2]=PT INSA: foodata[3]=1
x = 6: foodata[0]=71: foodata[1]=Iogurte Açucarado líquido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 7: foodata[0]=82: foodata[1]=Iogurte Aromatizado açucarado batido gordo: foodata[2]=PT INSA: foodata[3]=1
x = 8: foodata[0]=72: foodata[1]=Iogurte Aromatizado açucarado batido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 9: foodata[0]=77: foodata[1]=Iogurte Aromatizado açucarado líquido magro: foodata[2]=PT INSA: foodata[3]=1
x = 10: foodata[0]=73: foodata[1]=Iogurte Aromatizado açucarado líquido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 11: foodata[0]=78: foodata[1]=Iogurte Aromatizado açucarado sólido magro: foodata[2]=PT INSA: foodata[3]=1
x = 12: foodata[0]=74: foodata[1]=Iogurte Aromatizado açucarado sólido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 13: foodata[0]=79: foodata[1]=Iogurte Natural sólido magro: foodata[2]=PT INSA: foodata[3]=1
x = 14: foodata[0]=76: foodata[1]=Iogurte Natural sólido meio gordo: foodata[2]=PT INSA: foodata[3]=1
如您所见,索引只有一个字母,在本例中为"I"
。我尝试过只生成一个字母索引的其他输入,这种情况是唯一一个给我错误的情况。
知道为什么吗?
答案 0 :(得分:3)
这可以解决您的问题:
public int getPositionForSection(int section) {
if (section > sections.length - 1) {
return 0;
} else {
return alphaIndexer.get(sections[section]);
}
}