0

I made class Employee and I also made List<Employee> which accept object from the the class.

I put three objects in that list and I want to save them after close the app. I try to use SharedPreferences to put the list but it seem that SharedPreferences does not accept to put list in it. How can I do it?

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

    SharedPreferences data = this.getSharedPreferences("data",MODE_PRIVATE);
    SharedPreferences.Editor editor = data.edit();
    // I cant use editor to put list<Employee>



}
4

3 に答える 3

1
Shared Preferences takes only string so you can't keep an object into shared preferences. This problem comes if you want to send Employee object from one activity to another activity

これが私がそれを解決した方法です:次のライブラリをプロジェクトに追加します(最新のものを使用してください):

'com.google.code.gson:gson:1.7.2'

Employeeオブジェクトを文字列に変換し、共有設定に保存します。

Gson gson = new Gson();
String jsonString = gson.toJson(employeeObject);
//store this string in shared preferences and next time when you come back
//get string from shared preferences and convert this back to object
Gson gson = new Gson();
Employee example = gson.fromJson(jsonString, Employee.class);

疑問がある場合は以下にコメントしてください

于 2015-12-11T07:41:05.543 に答える