我保存arraylist大小和數據在sharedPreference.When我從sharedpreference檢索arraylist大小它給予什麼尺寸節省previous.But確切的大小,但我在保存Arraylist數據失敗。當我檢索arraylist數據它總是給列表列表的最後一項。如何在SharedPreferences中保存arrayList數據?
這裏是我救了ArrayList的大小和數據代碼:
ArrayList<Items> mArrayList1 = new ArrayList<Items>();
if(mArrayList1 == 0){
mStoreItem.setItem(id);
mArrayList1.add(mStoreItem);
int size = mArrayList1.size();
System.out.println("array list size : " + size);
MyPreferences.savePreferences(getActivity(),
"arraylist_size", Integer.toString(size));
for (int i = 0; i < mArrayList1.size(); i++) {
String id = mArrayList1.get(i)
.getItem();
MyPreferences.savePreferences(getActivity(),
"id", id);
}
} else if(mArrayList1 > 0){
mStoreItem.setItem(id);
mArrayList1.add(mStoreItem);
int size = mArrayList1.size();
System.out.println("arrayList size : " + size);
MyPreferences.savePreferences(getActivity(),
"arraylist_size", Integer.toString(size));
for (int i = 0; i < mArrayList1.size(); i++) {
String id = mArrayList1.get(i)
.getItem();
MyPreferences.savePreferences(getActivity(),
"id", id);
}
}
我在這裏取回我的ArrayList項目:
String getListSize = MyPreferences.savePreferences(getActivity(),
"arraylist_size");
System.out.println("arrayList size : " + getListSize);
if (!getListSize.equals("")) {
int listSize = Integer.parseInt(getListSize);
if (listSize > 0) {
for (int i = 0; i < listSize; i++) {
String getListitems = MyPreferences
.getPreferences(getActivity(), "id");
System.out.Println("list items : "+ getListItems);
}
}
}
這是我的代碼我怎麼能存儲什麼錯在我的代碼在sharedpreference任何一個arraylistitems請指導我...
請參閱此[save-arraylist-to-sharedpreferences](http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) –
請參閱http://stackoverflow.com/questions/12150597/ save-an-arraylist-of-strings-to-shared-preferences – rajshree