这是我的代码:
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String status = jsonObject.getString("success");
if (status.equals("1")) {
Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_LONG).show();
Intent i = new Intent(LoginActivity.this, MainActivity.class);
SharedPreferences.Editor editor = context.getSharedPreferences("UserDetail", 0).edit();
editor.clear();
editor.putString("mobile",edtmobilenumber.getText().toString());
editor.commit();
startActivity(i);
finish();
}
else if(status.equals("0")){
Toast.makeText(getApplicationContext(), "Invalid Credentials", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDailog.dismiss();
Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}) {
答案 0 :(得分:1)
保存价值
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Hello");
editor.putInt("value", 12);
editor.commit();
FOR FETCHING DATA BACK
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("value", 0); //0 is the default value.
}
答案 1 :(得分:0)
来自文档:
为这些首选项创建一个新的编辑器,通过该编辑器可以对首选项中的数据进行修改,并将这些更改原子地提交回SharedPreferences对象。
答案 2 :(得分:0)
您可以尝试以下代码:
首先声明:
SharedPreferences myPreference;
在onCreate()中:
myPreference =PreferenceManager.getDefaultSharedPreferences(this);
在OnResponse()中:
myPreference.edit().putString("mobile",edtmobilenumber.getText().toString()).apply();
答案 3 :(得分:0)
用于将共享首选项值保存为字符串
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
public void saveValue(String key, String value) {
SharedPreferences.Editor saveValue = prefs.edit();
saveValue.putString(key, value);
saveValue.apply();
}
获取储值
public String getValue(String key, String defaultValue) {
return prefs.getString(key, defaultValue);
}