当我注销sectionList时,有20个项目;出于某种原因,除了最后一项之外的所有内容都将输入到Array部分。有人可以看到这里有什么错吗?ArrayList数据没有正确传输到Java中的Array
更新:我发布了有问题的整个班级;它相对较短。它来自Android应用程序。
public class ItemAdapter extends ArrayAdapter<ItemObject> implements
SectionIndexer {
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
private LayoutInflater inflater;
public ItemAdapter(Context context, ItemObject[] objects) {
super(context, R.layout.item_row_layout, objects);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
alphaIndexer = new HashMap<String, Integer>();
int size = objects.length;
for (int i = 0; i < size; i++) {
ItemObject it = objects[i];
String name = it.name;
String s = name.substring(0, 1);
s = s.toUpperCase();
if (!alphaIndexer.containsKey(s)) {
alphaIndexer.put(s, i);
}
}
Set<String> sectionLetters = alphaIndexer.keySet();
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
sections = sectionList.toArray(new String[sectionList.size()]);
}
为了更快提供更好的帮助,请发布[SSCCE](http://sscce.org/)。 –
对我来说似乎很好。 –
@KickingLettuce你可以使用'ArrayList.toArray'。另外,如果你想要订购密钥,可以使用'TreeMap'。 – oldrinb