我使用共享首選項類來保存登錄後用戶的信息,以便以後更新我使用的信息保存在共享首選項類中,但當我更新用戶信息時,I必須從配置文件註銷,然後登錄查看更改。任何人都可以幫助我在更新用戶信息時如何更改SharedPrefManager類中的信息? Bellow是SharedPrefManager類和用戶更新類。在更新用戶信息時更新共享首選項
public class SharedPrefManager {
private static SharedPrefManager mInstance;
private static Context mCtx;
private static final String SHARED_PREF_NAME = "mysharedpref";
private static final String KEY_USERNAME = "name";
private static final String KEY_USER_EMAIL = "email";
private static final String KEY_USER_ID = "id";
private static final String KEY_USER_DESCRIPTION = "description";
private SharedPrefManager(Context context) {
mCtx = context;
}
public static synchronized SharedPrefManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new SharedPrefManager(context);
}
return mInstance;
}
public boolean userLogin(int id, String name, String email , String description){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(KEY_USER_ID, id);
editor.putString(KEY_USERNAME, name);
editor.putString(KEY_USER_EMAIL, email);
editor.putString(KEY_USER_DESCRIPTION, description);
editor.apply();
return true;
}
public boolean isLoggedIn(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
if(sharedPreferences.getString(KEY_USERNAME, null) != null){
return true;
}
return false;
}
public boolean logout(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
return true;
}
public String getUsername(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_USERNAME, null);
}
public String getUserEmail(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_USER_EMAIL, null);
}
public String getUserDesc(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_USER_DESCRIPTION, null);
}
}
這是用戶更新類。
public class EditProfile extends AppCompatActivity implements View.OnClickListener {
EditText name, description;
String email;
private ProgressDialog progressDialog;
Button update, delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
name = (EditText) findViewById(R.id.editText);
description = (EditText) findViewById(R.id.editDes);
update = (Button) findViewById(R.id.editUpdate);
delete = (Button) findViewById(R.id.DeletePro);
name.setText(SharedPrefManager.getInstance(this).getUsername());
description.setText(SharedPrefManager.getInstance(this).getUserDesc());
email = SharedPrefManager.getInstance(this).getUserEmail();
progressDialog = new ProgressDialog(this);
update.setOnClickListener(this);
delete.setOnClickListener(this);
}
private void updateUser() {
final String username = name.getText().toString().trim();
final String desc = description.getText().toString().trim();
if (username == "" || desc.length()>100) {
Toast.makeText(getApplicationContext(),"Invalid User Name or Description is exceeding thr limit",Toast.LENGTH_SHORT).show();
} else {
progressDialog.setMessage("Updating Information ...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Constants.URL_updatePro,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", username);
params.put("email", email);
params.put("description", desc);
return params;
}
};
RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
finish();
}
}
@Override
public void onClick(View v) {
if (v == update) {
updateUser();
}
if (v == delete)
{
startActivity(new Intent(EditProfile.this, DeleteProfile.class));
finish();
}
}
}
我用它在許多地方,所以只有name.setText也無濟於事。 – Tariq
對不起,共享首選項沒有自動調用方法。你必須盡你所能去做。 –
似乎是這樣,謝謝 – Tariq