【发布时间】:2014-04-02 13:12:41
【问题描述】:
我的数组列表有点问题,我需要一个解决方法来避免 IndexOutOfBoundsException 异常。
这是我添加项目的方式:
for(Map.Entry<String,?> entry : getAll().entrySet()){ // this is a map with all my sharedprefs
list.add((Integer) entry.getValue(), myItem);
}
我的共享首选项以以下方式存储:
<int name="test0" value="0" />
<int name="test1" value="1" />
<int name="test2" value="2" />
所以我需要根据 int 值将项目添加到我的数组列表中。 问题出现是因为 SharedPrefereces 映射以随机方式存储值,所以当它运行时
list.add((Integer) entry.getValue(), myItem);
它抛出异常,因为它需要从 0 开始。
有人有解决办法吗?谢谢
【问题讨论】:
-
在哪里定义
list。 -
这是一个简单的 ArrayList
。它不会抛出 npe! -
尝试做
ArrayList<String> arr = new ArrayList<String>(); -
你为什么不直接使用
SharedPreference?当有一个非常好的课程可以做您已经尝试做的事情时,这种“解决方法”似乎不必要地复杂。 -
MAP 条目将按自然顺序获取值.. 字母顺序..
标签: android exception arraylist sharedpreferences indexoutofboundsexception