【发布时间】:2016-08-09 08:15:24
【问题描述】:
我使用这两种方法来保存和加载我的数组列表,我需要在每个项目动态添加到数组后保存我的数组列表。
public void addItems(int howMany){
if (howMany > 0) {
int lastInsertedIndex = 11;
for (int i = lastInsertedIndex + 1; i <= lastInsertedIndex + howMany; i++) {
mList.add("Item " + i);
notifyItemInserted(mList.size() - 1);
}
lastInsertedIndex = lastInsertedIndex + howMany;
}
}
还有我的保存和加载方法:
public boolean saveArray() {
SharedPreferences sp = this.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Set<String> set = new HashSet<String>();
set.addAll(mainListAdapter.mList);
editor.putStringSet("list", set);
return editor.commit();
}
public ArrayList<String> getArray() {
SharedPreferences sp = context.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
//NOTE: if shared preference is null, the method return empty Hashset and not null
Set<String> set = sp.getStringSet("list", new HashSet<String>());
return new ArrayList<String>(set);
}
我的问题是:当我添加一个带有按钮的新项目时,没有任何反应,并且数组列表已满,如何解决这个问题?
【问题讨论】:
-
我使用 for 循环将列表保存到共享首选项,其中键作为列表中的索引号。我不能从我的手机编码,虽然它应该很容易搜索。类似于 put.String(i, mList.get(i));