我正在创建一个具有用户数据配置文件部分的应用程序。一个人可以更新数据,如大学,大学,分支机构。当用户登录应用程序时,我已经以共享首选项保存了这些数据,并且从该共享首选项数据中提供了用户的所有信息。如何立即更改首选项数据并设置为整个应用程序。我是开发领域的新手,需要帮助才能完成任务。
以下是我更新用户数据活动的代码
public void edit_user_data(){
final String str_new_name = _edit_prof_name_.getText().toString().trim();
linearLayout.setVisibility(View.VISIBLE);
ObjectAnimator flip = ObjectAnimator.ofFloat(imgSpoon, "rotationY", 0f, 180f);
flip.setDuration(800);
flip.setRepeatCount(Animation.INFINITE);
flip.start();
StringRequest strReq = new StringRequest(Request.Method.POST,
URLconstant.USER_UPDATE, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
linearLayout.setVisibility(View.INVISIBLE);
try {
JSONObject data = new JSONObject(response);
JSONObject updatedata = data.optJSONObject("data");
int intError = updatedata.getInt("status");
boolean error = (intError > 0) ? true : false;
if(intError == 1){
String msg = updatedata.optString("msg");
Toast.makeText(ActivityUserEditProfile.this, msg, Toast.LENGTH_SHORT).show();
JSONArray data_array = new JSONArray("data");
for (int i = 0;i<=data_array.length();i++){
JSONObject data_obj = data_array.getJSONObject(i);
/* UserProfileData userdata = new UserProfileData();
userdata.setFullname(data_obj.getString("fullname"));
userdata.setUniverId(data_obj.getString("univer_id"));
userdata.setMuniversity(data_obj.getString("university"));
userdata.setCollegeid(data_obj.getString("collegeid"));
userdata.setMcollege(data_obj.getString("college"));
userdata.setBranchid(data_obj.getString("branchid"));
userdata.setMbranch(data_obj.getString("branch"));
userdata.setSemid(data_obj.getString("semid"));
userdata.setMsemester(data_obj.getString("semester"));
userdata.setUserImage(data_obj.getString("user_image"));
userdata.setUserId(data_obj.getString("user_id"));
userdata.setPassword(data_obj.getString("password"));
updated_info(data_obj);
SharedPreferences preferences = getApplicationContext().getSharedPreferences(SHARED_PRE_LOGIN_REGISTRATION, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
*//* preferences.edit().remove(FULL_NAME);
preferences.edit().remove(UNIVER_ID);
preferences.edit().remove(BRANCH_ID);
preferences.edit().remove(COLLEGE_ID);
preferences.edit().remove(SEM_ID);*//*
editor.clear();
editor.commit();
PreferenceHelper preferenceHelper = new PreferenceHelper();
preferenceHelper.getInstance(context).setBranchid(userdata.getBranchid());
preferenceHelper.getInstance(context).setCollegeid(userdata.getCollegeid());
preferenceHelper.getInstance(context).setEnrollno(userdata.getEnrollno());
preferenceHelper.getInstance(context).setFullname(userdata.getFullname());
preferenceHelper.getInstance(context).setMobile(userdata.getMobile());
preferenceHelper.getInstance(context).setSemid(userdata.getSemid());
preferenceHelper.getInstance(context).setUniverId(userdata.getUniverId());
preferenceHelper.getInstance(context).setUserId(userdata.getUserId());
preferenceHelper.getInstance(context).setUserImage(userdata.getUserImage());
preferenceHelper.getInstance(context).setMuniversity(userdata.getMuniversity());
preferenceHelper.getInstance(context).setMcollege(userdata.getMcollege());
preferenceHelper.getInstance(context).setMbranch(userdata.getMbranch());
preferenceHelper.getInstance(context).setMsemester(userdata.getMsemester());
preferenceHelper.getInstance(context).setPassword(userdata.getPassword());*/
}
}else{
String msg = updatedata.optString("msg");
Toast.makeText(ActivityUserEditProfile.this, msg, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
// pDialog.hide();
linearLayout.setVisibility(View.INVISIBLE);
}
}){
//adding parameters to the request
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("fullname",str_new_name);
params.put("univer_id",u_id);
params.put("collegeid",c_id);
params.put("branchid",b_id);
params.put("semid",s_id);
params.put("mobile",PreferenceHelper.getInstance(getApplicationContext()).getMobile());
return params;
}
};
MySingleton.getInstance(this).addToRequestQueue(strReq);
}
答案 0 :(得分:0)
为此你可以这样做
SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// your stuff here
}
};
有关详细信息,请按照android developer Documentation
进行操作