2017-08-27 87 views
0

我使用共享首選項類來保存登錄後用戶的信息,以便以後更新我使用的信息保存在共享首選項類中,但當我更新用戶信息時,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(); 
     } 
    } 
} 

回答

0

檢查此鏈接:Answered here

我展示了一個人如何實現sharedPrefs,這是相當簡單的,更新的喜好和每次重新初始化的活動或片段你的代碼應該自動檢索無論是時間存儲在SharedPrefs中。這意味着,如果您在活動A中更新用戶名,則當您加載活動B時,它將獲取sharedPrefs並找到此新名稱(假設您的getSharedPrefs代碼位於片段/活動的onCreate或onResume方法中)。

如果在活動中沒有看到sharedPrefs信息更新,則可能是因爲活動不刷新(或者因爲您沒有告訴它,或者因爲某些內容已損壞)。您可以嘗試向活動添加一個「刷新」按鈕來測試此操作,這將重新啓動活動,而不是註銷/進入 - 與刷新操作相同。

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; 
      } 

希望這將工作

+0

我用它在許多地方,所以只有name.setText也無濟於事。 – Tariq

+0

對不起,共享首選項沒有自動調用方法。你必須盡你所能去做。 –

+0

似乎是這樣,謝謝 – Tariq