如何使用共享首选项来保存从数据库获取的数据? [复制]
Posted
技术标签:
【中文标题】如何使用共享首选项来保存从数据库获取的数据? [复制]【英文标题】:How to use shared preferences to save data that get from database? [duplicate] 【发布时间】:2016-11-24 12:30:37 【问题描述】:我有这个按钮代码,它连接到数据库
bLogin.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
// Response received from the server
Response.Listener<String> responseListener = new Response.Listener<String>()
@Override
public void onResponse(String response)
try
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success)
String name = jsonResponse.getString("name");
String email = jsonResponse.getString("email");
String alamat = jsonResponse.getString("alamat");
int notelp = jsonResponse.getInt("notelp");
String username = jsonResponse.getString("username");
String password = jsonResponse.getString("password");
Intent intent = new Intent(MainActivity.this, adminarea.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("alamat", alamat);
intent.putExtra("notelp", notelp);
intent.putExtra("username", username);
intent.putExtra("password", password);
MainActivity.this.startActivity(intent);
else
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Login Gagal")
.setNegativeButton("Ulangi", null)
.create()
.show();
catch (JSONException e)
e.printStackTrace();
;
我有一个名为 admin 的表,其中包含字段名称、电子邮件等。我如何从数据库中获取数据,然后将我的数据(如姓名、电子邮件、alamat 和其他数据)保存到共享偏好中。那你能帮我把它叫到另一个活动吗?
【问题讨论】:
从 db 迁移到共享首选项的任何原因? 这么多教程可用看看这个***.com/questions/23024831/… 【参考方案1】:首先你必须创建SharedPreferences
SharedPreferences pref = getApplicationContext().getSharedPreferences("YOUR_PREF_NAME", MODE_PRIVATE);
Editor edt = pref.edit();
优先增加价值:
edt.putString("name", name);
edt.putString("email", email);
edt.putString("alamat", alamat);
edt.putString("notelp", notelp);
edt.putString("username",username);
// Save your changes
edt.commit();
现在从偏好中获取数据:
String name=pref.getString("name", null);
String email=pref.getString("email", null);
String alamat=pref.getString("alamat", null);
String notelp=pref.getString("notelp", null);
String username=pref.getString("username", null);
如果要清除偏好数据:
edt.clear();
edt.commit();
【讨论】:
感谢您的回答【参考方案2】:如下所示保存。
SharedPreferences getPref = PreferenceManager.getDefaultSharedPreferences(this);
Intent intent = new Intent(MainActivity.this, adminarea.class);
intent.putExtra("name", name);
getPref.edit().putString("name",name).apply();
intent.putExtra("email", email);
getPref.edit().putString("email",name).apply();
intent.putExtra("alamat", alamat);
getPref.edit().putString("alamat",name).apply();
intent.putExtra("notelp", notelp);
intent.putExtra("username", username);
intent.putExtra("password", password);
MainActivity.this.startActivity(intent);
这样检索。
SharedPreferences getPref = PreferenceManager.getDefaultSharedPreferences(this);
String name = getPref.getString("name","");
String email = getPref.getString("email","");
String alamat = getPref.getString("alamat","");
【讨论】:
@Shail 运行它时的结果这一行 SharedPreferences getPref = PreferenceManager.getDefaultSharedPreferences(this);错误:不兼容的类型:> 无法转换为 Context @SiiAnnDie 请在此处发布您的代码您如何称呼它,您可以上下文创建它的上下文。 我的问题已经解决了...谢谢@Sohail Zahid 工作完美,谢谢队友【参考方案3】:创建一个类 SharePrefUtil
private Context context;
private SharedPreferences prefs;
public SharePrefUtil(Context mContext)
this.context = mContext;
prefs = this.context.getSharedPreferences("your package name", Context.MODE_PRIVATE);
public void setValueInSharePref(String keyName, String value)
prefs.edit().putString(keyName, value).apply();
public String getValueFromSharePref(String keyName)
return prefs.getString(keyName, "");
当您想要存储值时 只需在您的活动中创建一个类对象即可获取或设置一个值 例如
SharePreUtil shef = new SharePreUtil(this);
你什么时候在活动中设置一个值
shef.setValueInSharePref("key", "yourvalue");
当你想获得价值时
shef.getValueFromSharePref("key");
一切顺利...
【讨论】:
【参考方案4】:onCreate() method
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
to get all data server after
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(name, name);
editor.putString(email, email);
.....
editor.commit();
【讨论】:
【参考方案5】:在https://developer.android.com/training/basics/data-storage/shared-preferences.html引用培训指南
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
阅读
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
【讨论】:
【参考方案6】:在您的其他活动中......首先从意图接收数据......像这样
String name = getIntent().getExtras().getInt("name");
现在,将这些数据保存到共享首选项中......要做到这一点......你需要 sharedPreferences.Editor 来写入数据......像这样......
SharedPreferences sp = Your_Activity.this.getSharedPreferences("SharedPreferences_Name", Context.MODE_PRIVATE);
SharedPreferences.Editor spwriter = sp.edit();
spwriter.putString("name",name);
spwriter.commit();
【讨论】:
【参考方案7】:if (success)
// once you retrieve all the required data
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// Writing data to SharedPreferences
Editor editor = prefs.edit();
editor.putString("name", name);
editor.putString("email", email);
editor.putInt("notelp", notelp);
editor.commit();
// start your AdminArea activity here
检索数据 inn AdminArea 类:
// Reading from SharedPreferences
String value = prefs.getString("name", "DEFAULT_STRIGN"); // if the given key not found, it'll take the default string as value
int intValue = prefs.getInt("notelp", -1); // instead of -1, you can give any default value
希望这会有所帮助:)
【讨论】:
【参考方案8】:您可以在活动类的 SharedPreferences 中保存数据,如下所示:
SharedPreferences sharedPreferences = getSharedPreferences("USER_INFO", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", name);
editor.putString("email", alamat);
editor.putString("alamat", alamat);
editor.commit();
在您想要获取它们的活动中,请执行以下操作:
SharedPreferences sharedPreferences = getSharedPreferences("USER_INFO", Context.MODE_PRIVATE);
String name=sharedPreferences.getString("name", "abc");
String email=sharedPreferences.getString("email", "abc@gmail.com");
输入int,
editor.putInt("number",0);
然后检索它,
sharedPreferences.getInt("number",0);
【讨论】:
数据整数怎么样?以上是关于如何使用共享首选项来保存从数据库获取的数据? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 dart/flutter 中的共享首选项保存和获取列表列表