更新用户信息时更新共享首选项

时间:2017-08-27 19:11:05

标签: java android

我正在使用共享首选项类来保存用户登录后的信息,以便以后更新我使用的是我在共享首选项类中保存的信息但是当我更新用户信息时我必须从配置文件中注销然后登录查看更改。任何人都可以帮助我更新用户信息时如何更改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();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

点击此链接:Answered here

我向一个人展示了如何实现sharedPrefs,更新首选项非常简单,每次重新初始化一个活动或片段时,代码都应该自动检索存储在SharedPrefs中的内容。这意味着如果您更新活动A中的用户名,当您加载活动B时,它将获取sharedPrefs并找到这个新名称(假设您的getSharedPrefs代码在片段/活动的onCreate或onResume方法内)。

如果您没有在活动中看到sharedPrefs信息更新,可能是因为活动没有刷新(因为您没有告诉它,或者因为某些事情已经破坏)。您可以尝试在活动中添加一个“刷新”按钮来测试它,这将重新启动活动,而不是注销/进入 - 这与刷新相同。

答案 1 :(得分:0)

我想在这里你得到更新个人资料信息。

 @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);
                name.setText(username);
                description.setText(desc);
                return params;
            }

希望这会起作用