我需要和包含原始类型设置的对象,以便能够通过从对象调用加载或保存方法来加载和保存Android共享首选项。
我创建了一个名为thePreferences.java的类,这个类有一些原始字段。我在这个类中创建了2x方法来从sharedPreferences加载和保存这个类。请参阅下面我班级中使用的代码:
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.JsonSerializer;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.Serializable;
public class thePreferences extends AppCompatActivity implements Serializable {
private static final String PREFS_NAME = "thePreferences";
private static final String PREF_TEXT = "pref_text";
public static final String TAG = "WPC";
public boolean loggedIn = false;
public int deviceId = 0;
public int userId = 0;
public String email = "";
public String deviceAlias = "";
public thePreferences() {
}
public thePreferences(boolean loggedIn, int deviceId, int userId, String email, String deviceAlias) {
this.loggedIn = loggedIn;
this.deviceId = deviceId;
this.userId = userId;
this.email = email;
this.deviceAlias = deviceAlias;
}
public boolean loadThePreferences(Context c) {
SharedPreferences pref = c.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
if (pref.contains(PREF_TEXT)) {
String theJson = pref.getString(PREF_TEXT, null);
Log.i(TAG, "The JSON string loaded from Shared Preferences : " + theJson);
thePreferences p = new Gson().fromJson(theJson, thePreferences.class);
this.deviceAlias = (p.deviceAlias);
this.deviceId = (p.deviceId);
this.email = (p.email);
this.loggedIn = (p.loggedIn);
return true;
} else {
Log.i(TAG, "Could not load SharedPreferences, value does not exist.");
return false;
}
}
public static boolean saveThePreferences(Context c,thePreferences p){
Gson gson = new Gson();
String theJson = gson.toJson(p);
c.getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_TEXT,theJson)
.commit();
return true;
}
@Override
public String toString() {
return "thePreferences{" +
"loggedIn=" + this.loggedIn +
", deviceId=" + this.deviceId +
", userId=" + this.userId +
", email='" + this.email + '\'' +
", deviceAlias='" + this.deviceAlias + '\'' +
'}';
}
}
然后我尝试从另一个活动调用此加载并保存首选项,如下所示:
contextOfApplication = getApplicationContext();
thePreferences preferencesObj = new thePreferences(true,12345,552441,"abc@123.com","theDeivceAlias");
preferencesObj.saveThePreferences(contextOfApplication,preferencesObj);
preferencesObj.loadThePreferences(contextOfApplication);
我无法通过saveThePreferences()方法执行它。它继续使用StackOverflow代码崩溃,我无法弄清楚原因。'
有人可以告诉我是否有其他办法可以做到这一点,或者说我出错了?
提前感谢
答案 0 :(得分:3)
首先,thePreferences是一个类,因此按惯例应该以大写字母开头。
接下来,您不应该尝试序列化整个活动。相反,尝试将您需要的数据存储为原始类型,并将它们作为原始类型。将整个类保存为json字符串在这里没有意义。
一种方法:
创建一个新类,其唯一目的是加载和保存所需的数据。 (就像你的thePreferences,但是把它变成一个没有活动继承的简单类。)
新类将首选项名称和键保存为静态字符串。
使用上下文实例化新类并将共享首选项预加载为最终成员
为您需要的单个值提供加载和设置方法
在活动中使用此类作为成员或方法区域设置变量
Ps。:删除gson和可序列化的东西
编辑:如果您确实希望将数据存储为json字符串,则sharedPreferences不是要使用的util。我不建议这样做,但是你应该将数据发送到服务器或将其存储在外部数据存储(SD卡等)上。
答案 1 :(得分:2)
将prefernecesObj传递给自己
long double
将功能更改为:
preferencesObj.saveThePreferences(contextOfApplication,preferencesObj);
然后这样打电话:
public boolean saveThePreferences(Context c){
Gson gson = new Gson();
String theJson = gson.toJson(this);
c.getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_TEXT,theJson)
.commit();
return true;
}