Android将ArrayList <object>保存为SharedPreference </object>

时间:2014-09-26 15:56:09

标签: java android arraylist

我正在尝试将包含POJO的ArrayList保存为SharedPreference,以便在用户重新进入应用程序时检索它,但它似乎不起作用。

这就是我的所作所为:

回调方法

ArrayList<myObject> myList;
ArrayList<Boolean> myobject_repeat;
MyListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myview);
    lw = (ListView) findViewById(R.id.lw);
    myList= new ArrayList<myObject>();
    myobject_repeat = new ArrayList<Boolean>();
    adapter = new MyListAdapter(this, myList);
    lw.setAdapter(adapter);
}

@Override
protected void onPause() {
    saveArray();
    super.onPause();
}

@Override
protected void onResume() {
    loadArray(this);
    super.onResume();
}

保存/加载数组的方法

public boolean saveArray() {
    SharedPreferences pref = PreferenceManager
            .getDefaultSharedPreferences(this);
    SharedPreferences.Editor edit = pref.edit();
    edit.putInt("list_size", myList.size());

    for (int i = 0; i < myList.size(); i++) {
        edit.remove("id" + i);
        edit.remove("repeat" + i);
        edit.putLong("id" + i, myList.get(i).getId());
        edit.putBoolean("repeat" + i, myList.get(i).isRepeat());
    }

    return edit.commit();
}

public void loadArray(Context c) {
    SharedPreferences pref = PreferenceManager
            .getDefaultSharedPreferences(c);
    myList.clear();
    myList2.clear();
    int listsize = pref.getInt("list_size", 0);

    for (int i = 0; i < listsize; i++) {
        MyObject tmpObject = new MyObject();
        tmpObject.setId(pref.getLong("id" + i, 0));
        tmpObject.setRepeat(pref.getBoolean("repeat" + i, false));
        myList.add(tmpObject);
    }

}

这是我向数组添加对象的地方

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent result) {
    super.onActivityResult(requestCode, resultCode, result);

    if (resultCode == RESULT_OK) {
        int hour = result.getExtras().getInt("hour");
        int minute = result.getExtras().getInt("minute");
        boolean repeat = result.getExtras().getBoolean("repeat");
        setUpObject(hour, minute, repeat);
    }
}

private void setUpObject(int hour, int minute, boolean repeat) {

    //Doing magic tricks

    MyObject tmpObject= new MyObject ();
    tmpObject.setHours(hour);
    tmpObject.setMinutes(minute);
    tmpObject.setRepeat(repeat);
    tmpObject.setId(timeInMs);

    myList.add(tmpObject);
    adapter.remove(tmpObject);
    adapter.add(tmpObject);
    adapter.notifyDataSetChanged();
}

这似乎不起作用。当我调试它时,列表是空的。如果我在没有加载/保存方法的情况下运行应用程序,它按预期工作,但正如您所看到的,列表将不会保留在SharedPreferences中,因此当用户导航回我的活动时不可用。

非常感谢任何帮助。

马库斯

3 个答案:

答案 0 :(得分:3)

您可以使用GSON library

您可以将ArrayList转换为字符串并存储到Preference,然后将该字符串转换为相同的ArrayList。

   //-- code to convert list to string using Gson
     Type listType = new TypeToken<List<myObject>>() {}.getType();
     Gson gson = new Gson();
     String json = gson.toJson(target, listType);
    // store this string to shared preference


    //-- code to convert this string to list
    String json = gson.toJson(target, listType);
    List<String> target2 = gson.fromJson(json, listType);

答案 1 :(得分:1)

我在这里发现了问题:在onActivityResult之后调用onPause,它用一个空数组覆盖我的数组。

答案 2 :(得分:0)

很抱歉,您在行中添加 tmpAlarm

myList.add(tmpAlarm);

应该是

myList.add(tmpObject);

public void loadArray(Context c) {
  SharedPreferences pref = PreferenceManager
        .getDefaultSharedPreferences(c);
  myList.clear();
  myList2.clear();
  int listsize = pref.getInt("list_size", 0);

  for (int i = 0; i < listsize; i++) {
    MyObject tmpObject = new MyObject();
    tmpObject.setId(pref.getLong("id" + i, 0));
    tmpObject.setRepeat(pref.getBoolean("repeat" + i, false));
    myList.add(tmpAlarm);
 }
}