更新一个会话管理器

Update a sessionManager

你好,

我在 sessionManager 中存储了一长串用户信息。 我正在寻找无需再次创建 createUserSession

即可更新用户信息的最佳方法

我有那个会话管理器class:

import android.content.Context;
import android.content.SharedPreferences;

public class SessionManager {

private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private final static String PREFS_Name = "app_prefs";
private final static int PRIVATE_MODE = 0;
private final static String IS_LOGGED = "isLogged";
private final static String TEL_EMAIL = "tel_email";
private final static String PASSWORD = "password";
private final static String ID = "id";
private final static String FIRST_NAME = "first_name";
private final static String LAST_NAME = "last_name";
private final static String TEL = "tel";
.........
.......
.......

public SessionManager(Context context) {
    prefs = context.getSharedPreferences(PREFS_Name, PRIVATE_MODE);
    editor = prefs.edit();
    editor.apply();
}

public boolean isLogged() {
    return prefs.getBoolean(IS_LOGGED, false);
}

public String getTelEmail() {
    return prefs.getString(TEL_EMAIL, null);
}
public String getTel() {
    return prefs.getString(TEL, null);
}
public String getAvatar() {
    return prefs.getString(AVATAR, null);
}
public String getBack() {
    return prefs.getString(BACK, null);
}
public String getIntroduction() {
    return prefs.getString(INTRODUCTION, null);
}
public String getIntroductionAudience() {
    return prefs.getString(INTRODUCTION_AUDIENCE, null);
}
public String getInfosIntroAudience() {
    return prefs.getString(INFOS_INTRO_AUDIENCE, null);
}
public String getWallVisibilityAudience() {
    return prefs.getString(WALL_VISIBILITY_AUDIENCE, null);
}
public String getWallPostInAudience() {
    return prefs.getString(WALL_POST_IN_AUDIENCE, null);
}


public void createUserSession(String id, String tel_email, String password, String first_name, String last_name,
                              String sex, String tel, String email,...........) {
    editor.putBoolean(IS_LOGGED, true);
    editor.putString(ID, id);
    editor.putString(TEL_EMAIL, tel_email);
    editor.putString(PASSWORD, password);
    editor.putString(FIRST_NAME, first_name);
    ......
    ......
    ......
    editor.putString(TEL, tel);
    ......
    ......
    ......
    editor.commit();
}

public void logOut() {
    editor.clear().commit();
    }
}

当我想编辑电话时,我必须再次创建 sessionManager,如下所示:

  // Update SessionManager
                        sessionManager.createUserSession(sessionManager.getId(),
                                sessionManager.getTelEmail(),
                                sessionManager.getPassword(),
                                sessionManager.getFirstName(),
                                sessionManager.getLastName(),
                                sessionManager.getSex(),
                                NewTel,                          // here i want to update the value
                                sessionManager.getEmail(),
                                sessionManager.getCountry(),
                                sessionManager.getIso2(),
                                sessionManager.getDay(),
                                sessionManager.getMonth(),
                                sessionManager.getYear(),
                                sessionManager.getAvatar(),
                                sessionManager.getBack(),
                                sessionManager.getIntroduction(),
                                sessionManager.getIntroductionAudience(),
                                sessionManager.getInfosIntroAudience(),
                                sessionManager.getTelEmailAudience(),
                                sessionManager.getSituation(),
                                sessionManager.getLive(),
                                sessionManager.getFrom(),
                                sessionManager.getSchool(),
                                sessionManager.getSchool_2(),
                                sessionManager.getSchool_3(),
                                sessionManager.getSchool_4(),
                                sessionManager.getSchool_5(),
                                sessionManager.getJob(),
                                sessionManager.getJob_2(),
                                sessionManager.getJob_3(),
                                sessionManager.getJob_4(),
                                sessionManager.getJob_5(),
                                sessionManager.getPassion(),
                                sessionManager.getTotalFriends(),
                                sessionManager.getWallVisibilityAudience(),
                                sessionManager.getWallPostInAudience(),
                                sessionManager.getWhoCanFollowMe(),
                                sessionManager.getWhoCanIdentifyMe(),
                                sessionManager.getWhoCanIdentifyMe2(),
                                sessionManager.getWhoCanIdentifyMe3(),
                                sessionManager.getLast_Post_Audience(),
                                sessionManager.getFriends_Audience(),
                                sessionManager.getShowOrHideGroupPageEvent(),
                                sessionManager.getShowOrHideUserOwnGroupPageEvent());

我怎样才能只更改电话而不必再次创建 sessionManager.createUserSession

我怎样才能避免再次使用所有值重复创建 sessionManager.createUserSession

谢谢。

像这样单独做一个函数

public void setTel(String tel) {
    SharedPreferences.Editor editor = prefs.edit();
     editor.putString(TEL, tel);
}

那就只调用这个函数;

  sessionManager.setTel("901234567")