gpt4 book ai didi

java - LIstView SectionIndexer 给出错误 ArrayIndexOutOfBoundsException

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:33:51 26 4
gpt4 key购买 nike

我已经使用 SectionIndexer 实现了一个 ListView。它在 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”。我尝试过其他仅产生一个字母索引的输入,这是唯一给我错误的情况。

知道为什么吗?

最佳答案

这可以解决您的问题:

public int getPositionForSection(int section) {

if (section > sections.length - 1) {
return 0;
} else {

return alphaIndexer.get(sections[section]);
}
}

关于java - LIstView SectionIndexer 给出错误 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21662322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com