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

huangapple go评论74阅读模式
英文:

Android: sharedPreference in Fragment,not read data from Class

问题

i have this class for SharedPref work

    package com.box.a100rados.Data;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    
    import static android.content.Context.MODE_PRIVATE;
    
    
    public class UserAccessSession {
    
        private SharedPreferences sharedPref;
        private Editor editor;
    
    
        private static final String FILTER_RADIUS_DISTANCE       = "FILTER_RADIUS_DISTANCE";
        private static final String Name = "Name";
        private static final String Photo = "Photo";
        private static final String Rank = "Rank";
        private static final String Sid = "Sid";
        private static final String Ban = "Ban";
        private static final String Latitude = "Latitude";
        private static final String Longitude = "Longitude";
    
        private static final String SHARED = "UserData";
        private static UserAccessSession instance;
    
        public static UserAccessSession getInstance(Context context) {
            if(instance == null)
                instance = new UserAccessSession(context);
            return instance;
        }
    
        public UserAccessSession(Context context) {
            sharedPref = context.getSharedPreferences(SHARED, MODE_PRIVATE);
            editor = sharedPref.edit();
        }
    
        public void storeUserSession(UserSession session) {
            editor.putString(Name, session.getUsername());
            editor.putString(Photo, session.getPhoto_url());
            editor.putInt(Rank, session.getUser_Rank());
            editor.putString(Sid, session.getUser_id());
            editor.putBoolean(Ban, session.getUser_Ban());
            editor.putString(Latitude, session.getLatitude());
            editor.putString(Longitude, session.getLongitude());
            editor.apply();
        }
    
        public void clearUserSession() {
            editor.putString(Name, null);
            editor.putString(Photo, null);
            editor.putInt(Rank, 1);
            editor.putString(Sid, null);
            editor.putBoolean(Ban, false);
            editor.putString(Latitude, null);
            editor.putString(Longitude, null);
            editor.apply();
        }
    
        public UserSession getUserSession() {
            UserSession session = new UserSession();
            session.setUsername( sharedPref.getString(Name, null) );
            session.setPhoto_url( sharedPref.getString(Photo, null) );
            session.setUser_Rank( sharedPref.getInt(Rank, 1) );
            session.setUser_id( sharedPref.getString(Sid, "") );
            session.setUser_Ban( sharedPref.getBoolean(Ban, false) );
            session.setLatitude(sharedPref.getString(Latitude,"59.946104"));
            session.setLongitude(sharedPref.getString(Longitude,"30.306048"));
            return session;
        }
    
        public void setFilterDistance(float radius) {
            editor.putFloat(FILTER_RADIUS_DISTANCE, radius);
            editor.commit();
        }
    
        public float getFilterDistance() {
            return  sharedPref.getFloat(FILTER_RADIUS_DISTANCE, 0);
        }
    }

How i call:

    UserAccessSession tes = UserAccessSession.getInstance(getActivity().getApplicationContext());
                    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

    UserSession userSession = new UserSession();
                    UserAccessSession session = UserAccessSession.getInstance(MainActivity.this);
                    String sid = User_SID;
                    userSession.setUsername(UserName);
                    userSession.setPhoto_url(Photo_URL);
                    userSession.setUser_Rank(Rank);
                    userSession.setUser_id(sid);
                    session.storeUserSession(userSession);

Coordinates i add like this:

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

> Thank you for help. I apologize for the possible repetition of the
> question. I speak English poorly and its hard to find information.
英文:

i have this class for SharedPref work

package com.box.a100rados.Data;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import static android.content.Context.MODE_PRIVATE;
public class UserAccessSession {
private SharedPreferences sharedPref;
private Editor editor;
private static final String FILTER_RADIUS_DISTANCE 		= "FILTER_RADIUS_DISTANCE";
private static final String Name = "Name";
private static final String Photo = "Photo";
private static final String Rank = "Rank";
private static final String Sid = "Sid";
private static final String Ban = "Ban";
private static final String Latitude = "Latitude";
private static final String Longitude = "Longitude";
private static final String SHARED = "UserData";
private static UserAccessSession instance;
public static UserAccessSession getInstance(Context context) {
if(instance == null)
instance = new UserAccessSession(context);
return instance;
}
public UserAccessSession(Context context) {
sharedPref = context.getSharedPreferences(SHARED, MODE_PRIVATE);
editor = sharedPref.edit();
}
public void storeUserSession(UserSession session) {
editor.putString(Name, session.getUsername());
editor.putString(Photo, session.getPhoto_url());
editor.putInt(Rank, session.getUser_Rank());
editor.putString(Sid, session.getUser_id());
editor.putBoolean(Ban, session.getUser_Ban());
editor.putString(Latitude, session.getLatitude());
editor.putString(Longitude, session.getLongitude());
editor.apply();
}
public void clearUserSession() {
editor.putString(Name, null);
editor.putString(Photo, null);
editor.putInt(Rank, 1);
editor.putString(Sid, null);
editor.putBoolean(Ban, false);
editor.putString(Latitude, null);
editor.putString(Longitude, null);
editor.apply();
}
public UserSession getUserSession() {
UserSession session = new UserSession();
session.setUsername( sharedPref.getString(Name, null) );
session.setPhoto_url( sharedPref.getString(Photo, null) );
session.setUser_Rank( sharedPref.getInt(Rank, 1) );
session.setUser_id( sharedPref.getString(Sid, "") );
session.setUser_Ban( sharedPref.getBoolean(Ban, false) );
session.setLatitude(sharedPref.getString(Latitude,"59.946104"));
session.setLongitude(sharedPref.getString(Longitude,"30.306048"));
return session;
}
public void setFilterDistance(float radius) {
editor.putFloat(FILTER_RADIUS_DISTANCE, radius);
editor.commit();
}
public float getFilterDistance() {
return  sharedPref.getFloat(FILTER_RADIUS_DISTANCE, 0);
}
}

How i call:

UserAccessSession tes = UserAccessSession.getInstance(getActivity().getApplicationContext());
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

UserSession userSession = new UserSession();
UserAccessSession session = UserAccessSession.getInstance(MainActivity.this);
String sid = User_SID;
userSession.setUsername(UserName);
userSession.setPhoto_url(Photo_URL);
userSession.setUser_Rank(Rank);
userSession.setUser_id(sid);
session.storeUserSession(userSession);

Coordinates i add like this:

userAccess = UserAccessSession.getInstance(MainActivity.this);
userSession = userAccess.getUserSession();
userSession.setLatitude(String.valueOf(location.getLatitude()));
userSession.setLongitude(String.valueOf(location.getLongitude()));
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

将change set更改为get

public UserSession getUserSession() {
    UserSession session = new UserSession();
    session.getUsername(sharedPref.getString(Name, null));
    session.getPhoto_url(sharedPref.getString(Photo, null));
    session.getUser_Rank(sharedPref.getInt(Rank, 1));
    session.getUser_id(sharedPref.getString(Sid, ""));
    session.getUser_Ban(sharedPref.getBoolean(Ban, false));
    session.getLatitude(sharedPref.getString(Latitude, "59.946104"));
    session.getLongitude(sharedPref.getString(Longitude, "30.306048"));
    return session;
}
英文:

change set to get

    public UserSession getUserSession() {
UserSession session = new UserSession();
session.getUsername( sharedPref.getString(Name, null) );
session.getPhoto_url( sharedPref.getString(Photo, null) );
session.getUser_Rank( sharedPref.getInt(Rank, 1) );
session.getUser_id( sharedPref.getString(Sid, "") );
session.getUser_Ban( sharedPref.getBoolean(Ban, false) );
session.getLatitude(sharedPref.getString(Latitude,"59.946104"));
session.getLongitude(sharedPref.getString(Longitude,"30.306048"));
return session;
}

答案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.

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

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

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

发表评论

匿名网友

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

确定