【发布时间】:2015-06-17 17:04:14
【问题描述】:
我有以下代码:
for (int i = 0; i < checkList.getCount(); i++) {
LinearLayout itemLayout = (LinearLayout) checkList.getChildAt(i);
CheckBox checkBox = (CheckBox) itemLayout.findViewById(R.id.checkbox);
if (checkBox.isChecked()) {
sharedPreferenceShopingList.removeShopingItem(getActivity(), i);
}
}
在 sharedPreferences 中:
public void removeShopingItem(Context context, int position) {
List<PlanerItems> planer = getShopingItems(context);
planer.remove(position);
saveShopingItem(context, planer);
}
我正在尝试删除所有选中的项目,但是,如果我将它们一个一个或几个项目删除,它会起作用,但是如果我尝试删除所有项目,则会出现以下异常:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.remove(ArrayList.java:403)
at com.thezec.zdravahrana.Utils.SharedPreferenceShopingList.removeShopingItem(SharedPreferenceShopingList.java:69)
at com.thezec.zdravahrana.Fragments.ShopingListFragment$9.onSelection(ShopingListFragment.java:271)
我被困在这个问题上,所以任何帮助都会很好。
这里是 getShopingItem:
public ArrayList<PlanerItems> getShopingItems(Context context) {
SharedPreferences settings;
List<PlanerItems> planer;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
if (settings.contains(PLANER)) {
String jsonFavorites = settings.getString(PLANER, null);
Gson gson = new Gson();
PlanerItems[] favoriteItems = gson.fromJson(jsonFavorites,
PlanerItems[].class);
planer = Arrays.asList(favoriteItems);
planer = new ArrayList<PlanerItems>(planer);
}
else {
return null;
}
return (ArrayList<PlanerItems>) planer;
}
【问题讨论】:
-
我认为问题出在 getShopingItems(context).... 如果你能发布那个 sn-p 那就太好了
-
我已经用方法编辑了帖子
-
checkList的数据类型是什么 -
checkList 是 ListView
标签: android checkbox sharedpreferences indexoutofboundsexception