我使用这两种方法保存并加载我的数组列表,并且我需要在每个项目动态添加到数组之后保存我的数组列表。如何使用共享偏好来保存数组列表项?
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)); – ziondreamt