android:如何使用共享首选项从其他活动获取数据

时间:2016-03-31 11:49:26

标签: android android-sharedpreferences

我想使用共享偏好从另一个活动获取数据,下面是我的LoginDao类,我已经成功地从json对象设置了数据。

public class LoginDao implements LoginListner {
    private ArrayList<LoginModel> alLogin;
    @Override
    public ArrayList<LoginModel> getLoginDeatilFromJson(JSONObject jsonObject) {
        try {
            alLogin = new ArrayList<LoginModel>();
            if (jsonObject != null) {

                LoginModel model = new LoginModel();

                model.setStrShiftee_id(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_SHIFTEE_ID));
                model.setStrFName(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_FIRST_NAME));
                model.setStrLName(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_LAST_NAME));
                model.setStrEmail(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_EMAIL));
                model.setStrSkill(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_SKILL));
                model.setStrAddress1(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_ADDRESS1));
                model.setStrAddress2(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_ADDRESS2));
                model.setStrCity(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_CITY));
                model.setStrCountryName(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_COUNTRY_NAME));
                model.setStrStateName(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_STATE_NAME));
                model.setStrPinCode(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_PINCODE));
                model.setStrContact(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_CONTACT1));
                model.setStrDegree(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_DEGREE));
                model.setStrCollege(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_COLLEGE));
                model.setStrYear(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_YEAR));
                model.setStrGrade(jsonObject.getString(WebField.LOGIN_USER.RESPONSE_GRADE));

                alLogin.add(model);

                Log.e("Checking Register Dao size", ">" + alLogin.size());
                Log.e("Checking Register Dao", ">" + alLogin);
                return alLogin;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return alLogin;
    }
}

这是我的模特课。

public class LoginModel implements Serializable {
    private String strFName;
    private String strLName;

    public String getStrFName() {
        return strFName;
    }
    public void setStrFName(String strFName) {
        this.strFName = strFName;
    }
    public String getStrLName() {
        return strLName;
    }
    public void setStrLName(String strLName) {
        this.strLName = strLName;
    }
}

我在SessionManager类的名称中使用了共享首选项来保存用户数据。

    public class SessionManager {
    private static final String PREF_NAME = "Session";
    public static final String KEY_FIRSTNAME = "firstName";
    public static final String KEY_LNAME = "lastName";
    public static final String KEY_EMAIL = "emailAddress";
    public static final String KEY_ADDRESS1 = "address1";
    public static final String KEY_ADDRESS2 = "address2";

    public static void saveUsersDetails(Context context, String strFname, String strLname, String strEmail, String strAddress1, String strAddress2) {
        try {
            SharedPreferences preferences = context.getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();

            editor.putString(KEY_FIRSTNAME, strFname);
            editor.putString(KEY_LNAME, strLname);
            editor.putString(KEY_EMAIL, strEmail);
            editor.putString(KEY_ADDRESS1, strAddress1);
            editor.putString(KEY_ADDRESS2, strAddress2);

            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static LoginModel getUserDetail(Context context) {
        LoginModel loginModel = new LoginModel();
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

        loginModel.setStrFName(preferences.getString(SessionManager.KEY_FIRSTNAME, ""));
        loginModel.setStrLName(preferences.getString(SessionManager.KEY_LNAME, ""));
    }
}

我使用了常见的SessionManager类来存储数据,我想从另一个活动中检索所有数据。

1 个答案:

答案 0 :(得分:0)

您可以从任何类或活动中获取数据,例如

LoginModel userData = new LoginModel();

userData = SessionManager.getUserDetail(MainActivity.this);

String UserName = userData.getStrFName();