我的arraylist出现了一个小问题,我需要一个解决方法来避免IndexOutOfBoundsException异常。从SharedPreferences向ArrayList添加项目时发生IndexOutOfBoundsException
这里是我如何加入项目:
for(Map.Entry<String,?> entry : getAll().entrySet()){ // this is a map with all my sharedprefs
list.add((Integer) entry.getValue(), myItem);
}
我sharedprefs存储在以下方式:
<int name="test0" value="0" />
<int name="test1" value="1" />
<int name="test2" value="2" />
所以我需要根据项目添加到我的ArrayList int值。 问题来,因为SharedPrefereces映射值存储在一个随机的方式,所以当它运行
list.add((Integer) entry.getValue(), myItem);
它抛出,因为它需要从0
任何人都开始有解决的办法的例外?由于
你在哪里定义'list'。 –
这是一个简单的ArrayList。它不会扔n!! –
iGio90
尝试做'ArrayList arr = new ArrayList ();' –