Android: 在Fragment中使用sharedPreference,在Class中无法读取数据。

huangapple 未分类评论43阅读模式
英文:

Android: sharedPreference in Fragment,not read data from Class

问题

  1. i have this class for SharedPref work
  2. package com.box.a100rados.Data;
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.content.SharedPreferences.Editor;
  6. import static android.content.Context.MODE_PRIVATE;
  7. public class UserAccessSession {
  8. private SharedPreferences sharedPref;
  9. private Editor editor;
  10. private static final String FILTER_RADIUS_DISTANCE = "FILTER_RADIUS_DISTANCE";
  11. private static final String Name = "Name";
  12. private static final String Photo = "Photo";
  13. private static final String Rank = "Rank";
  14. private static final String Sid = "Sid";
  15. private static final String Ban = "Ban";
  16. private static final String Latitude = "Latitude";
  17. private static final String Longitude = "Longitude";
  18. private static final String SHARED = "UserData";
  19. private static UserAccessSession instance;
  20. public static UserAccessSession getInstance(Context context) {
  21. if(instance == null)
  22. instance = new UserAccessSession(context);
  23. return instance;
  24. }
  25. public UserAccessSession(Context context) {
  26. sharedPref = context.getSharedPreferences(SHARED, MODE_PRIVATE);
  27. editor = sharedPref.edit();
  28. }
  29. public void storeUserSession(UserSession session) {
  30. editor.putString(Name, session.getUsername());
  31. editor.putString(Photo, session.getPhoto_url());
  32. editor.putInt(Rank, session.getUser_Rank());
  33. editor.putString(Sid, session.getUser_id());
  34. editor.putBoolean(Ban, session.getUser_Ban());
  35. editor.putString(Latitude, session.getLatitude());
  36. editor.putString(Longitude, session.getLongitude());
  37. editor.apply();
  38. }
  39. public void clearUserSession() {
  40. editor.putString(Name, null);
  41. editor.putString(Photo, null);
  42. editor.putInt(Rank, 1);
  43. editor.putString(Sid, null);
  44. editor.putBoolean(Ban, false);
  45. editor.putString(Latitude, null);
  46. editor.putString(Longitude, null);
  47. editor.apply();
  48. }
  49. public UserSession getUserSession() {
  50. UserSession session = new UserSession();
  51. session.setUsername( sharedPref.getString(Name, null) );
  52. session.setPhoto_url( sharedPref.getString(Photo, null) );
  53. session.setUser_Rank( sharedPref.getInt(Rank, 1) );
  54. session.setUser_id( sharedPref.getString(Sid, "") );
  55. session.setUser_Ban( sharedPref.getBoolean(Ban, false) );
  56. session.setLatitude(sharedPref.getString(Latitude,"59.946104"));
  57. session.setLongitude(sharedPref.getString(Longitude,"30.306048"));
  58. return session;
  59. }
  60. public void setFilterDistance(float radius) {
  61. editor.putFloat(FILTER_RADIUS_DISTANCE, radius);
  62. editor.commit();
  63. }
  64. public float getFilterDistance() {
  65. return sharedPref.getFloat(FILTER_RADIUS_DISTANCE, 0);
  66. }
  67. }
  68. How i call:
  69. UserAccessSession tes = UserAccessSession.getInstance(getActivity().getApplicationContext());
  70. UserSession test = tes.getUserSession();
  71. But in `test.getLatitude()` or `test.getLongitude()` i have Null value.
  72. The same sometimes happens in the main activity with `getUser_Rank` and `getUsername` or other values.
  73. What have I done wrong and how can I fix it?
  74. On the map, after re-opening, I get data from the SharedPref and the map camera moves where I need.
  75. P.S I add data like this
  76. UserSession userSession = new UserSession();
  77. UserAccessSession session = UserAccessSession.getInstance(MainActivity.this);
  78. String sid = User_SID;
  79. userSession.setUsername(UserName);
  80. userSession.setPhoto_url(Photo_URL);
  81. userSession.setUser_Rank(Rank);
  82. userSession.setUser_id(sid);
  83. session.storeUserSession(userSession);
  84. Coordinates i add like this:
  85. userAccess = UserAccessSession.getInstance(MainActivity.this);
  86. userSession = userAccess.getUserSession();
  87. userSession.setLatitude(String.valueOf(location.getLatitude()));
  88. userSession.setLongitude(String.valueOf(location.getLongitude()));
  89. userAccess.storeUserSession(userSession);
  90. > Thank you for help. I apologize for the possible repetition of the
  91. > question. I speak English poorly and its hard to find information.
英文:

i have this class for SharedPref work

  1. package com.box.a100rados.Data;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.content.SharedPreferences.Editor;
  5. import static android.content.Context.MODE_PRIVATE;
  6. public class UserAccessSession {
  7. private SharedPreferences sharedPref;
  8. private Editor editor;
  9. private static final String FILTER_RADIUS_DISTANCE = "FILTER_RADIUS_DISTANCE";
  10. private static final String Name = "Name";
  11. private static final String Photo = "Photo";
  12. private static final String Rank = "Rank";
  13. private static final String Sid = "Sid";
  14. private static final String Ban = "Ban";
  15. private static final String Latitude = "Latitude";
  16. private static final String Longitude = "Longitude";
  17. private static final String SHARED = "UserData";
  18. private static UserAccessSession instance;
  19. public static UserAccessSession getInstance(Context context) {
  20. if(instance == null)
  21. instance = new UserAccessSession(context);
  22. return instance;
  23. }
  24. public UserAccessSession(Context context) {
  25. sharedPref = context.getSharedPreferences(SHARED, MODE_PRIVATE);
  26. editor = sharedPref.edit();
  27. }
  28. public void storeUserSession(UserSession session) {
  29. editor.putString(Name, session.getUsername());
  30. editor.putString(Photo, session.getPhoto_url());
  31. editor.putInt(Rank, session.getUser_Rank());
  32. editor.putString(Sid, session.getUser_id());
  33. editor.putBoolean(Ban, session.getUser_Ban());
  34. editor.putString(Latitude, session.getLatitude());
  35. editor.putString(Longitude, session.getLongitude());
  36. editor.apply();
  37. }
  38. public void clearUserSession() {
  39. editor.putString(Name, null);
  40. editor.putString(Photo, null);
  41. editor.putInt(Rank, 1);
  42. editor.putString(Sid, null);
  43. editor.putBoolean(Ban, false);
  44. editor.putString(Latitude, null);
  45. editor.putString(Longitude, null);
  46. editor.apply();
  47. }
  48. public UserSession getUserSession() {
  49. UserSession session = new UserSession();
  50. session.setUsername( sharedPref.getString(Name, null) );
  51. session.setPhoto_url( sharedPref.getString(Photo, null) );
  52. session.setUser_Rank( sharedPref.getInt(Rank, 1) );
  53. session.setUser_id( sharedPref.getString(Sid, "") );
  54. session.setUser_Ban( sharedPref.getBoolean(Ban, false) );
  55. session.setLatitude(sharedPref.getString(Latitude,"59.946104"));
  56. session.setLongitude(sharedPref.getString(Longitude,"30.306048"));
  57. return session;
  58. }
  59. public void setFilterDistance(float radius) {
  60. editor.putFloat(FILTER_RADIUS_DISTANCE, radius);
  61. editor.commit();
  62. }
  63. public float getFilterDistance() {
  64. return sharedPref.getFloat(FILTER_RADIUS_DISTANCE, 0);
  65. }
  66. }

How i call:

  1. UserAccessSession tes = UserAccessSession.getInstance(getActivity().getApplicationContext());
  2. UserSession test = tes.getUserSession();

But in test.getLatitude() or test.getLongitude() i have Null value.
The same sometimes happens in the main activity with getUser_Rank and getUsername or other values.
What have I done wrong and how can I fix it?
On the map, after re-opening, I get data from the SharedPref and the map camera moves where I need.

P.S I add data like this

  1. UserSession userSession = new UserSession();
  2. UserAccessSession session = UserAccessSession.getInstance(MainActivity.this);
  3. String sid = User_SID;
  4. userSession.setUsername(UserName);
  5. userSession.setPhoto_url(Photo_URL);
  6. userSession.setUser_Rank(Rank);
  7. userSession.setUser_id(sid);
  8. session.storeUserSession(userSession);

Coordinates i add like this:

  1. userAccess = UserAccessSession.getInstance(MainActivity.this);
  2. userSession = userAccess.getUserSession();
  3. userSession.setLatitude(String.valueOf(location.getLatitude()));
  4. userSession.setLongitude(String.valueOf(location.getLongitude()));
  5. userAccess.storeUserSession(userSession);

> Thank you for help. I apologize for the possible repetition of the
> question. I speak English poorly and it’s hard to find information.

答案1

得分: 1

我猜由于您从不同的来源访问,出现了问题。

请改用默认的共享偏好设置,它会使用单个文件进行存储,而不是使用不同的文件。

将这个代码:context.getSharedPreferences(SHARED, MODE_PRIVATE);

替换为这个代码:PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

英文:

I guess as you are accessing is from different sources, issues occurs.

Use the default shared preference instead, which uses a single file for storage instead different ones.

Replace this context.getSharedPreferences(SHARED, MODE_PRIVATE);

with this PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

答案2

得分: 0

  1. change set更改为get
  2. public UserSession getUserSession() {
  3. UserSession session = new UserSession();
  4. session.getUsername(sharedPref.getString(Name, null));
  5. session.getPhoto_url(sharedPref.getString(Photo, null));
  6. session.getUser_Rank(sharedPref.getInt(Rank, 1));
  7. session.getUser_id(sharedPref.getString(Sid, ""));
  8. session.getUser_Ban(sharedPref.getBoolean(Ban, false));
  9. session.getLatitude(sharedPref.getString(Latitude, "59.946104"));
  10. session.getLongitude(sharedPref.getString(Longitude, "30.306048"));
  11. return session;
  12. }
英文:

change set to get

  1. public UserSession getUserSession() {
  2. UserSession session = new UserSession();
  3. session.getUsername( sharedPref.getString(Name, null) );
  4. session.getPhoto_url( sharedPref.getString(Photo, null) );
  5. session.getUser_Rank( sharedPref.getInt(Rank, 1) );
  6. session.getUser_id( sharedPref.getString(Sid, "") );
  7. session.getUser_Ban( sharedPref.getBoolean(Ban, false) );
  8. session.getLatitude(sharedPref.getString(Latitude,"59.946104"));
  9. session.getLongitude(sharedPref.getString(Longitude,"30.306048"));
  10. return session;
  11. }

答案3

得分: 0

我很愚蠢。

userAccess = UserAccessSession.getInstance(MainActivity.this);
userSession = userAccess.getUserSession();
userSession.setLatitude(String.valueOf(location.getLatitude()));
userSession.setLongitude(String.valueOf(location.getLongitude()));
userAccess.storeUserSession(userSession);
这个调用清除了现有数据。
将坐标传输到一个单独的方法中,一切正常运行。

英文:

I'm stupid.

  1. userAccess = UserAccessSession.getInstance(MainActivity.this);
  2. userSession = userAccess.getUserSession();
  3. userSession.setLatitude(String.valueOf(location.getLatitude()));
  4. userSession.setLongitude(String.valueOf(location.getLongitude()));
  5. userAccess.storeUserSession(userSession);

This call cleared existing data.
Transferred coordinates to a separate method, everything worked.

huangapple
  • 本文由 发表于 2020年4月5日 17:04:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/61040252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定