简体   繁体   English

Android:片段中的共享首选项,不从 Class 读取数据

[英]Android: sharedPreference in Fragment,not read data from Class

i have this class for SharedPref work我有这个 class 用于 SharedPref 工作

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.但在test.getLatitude()test.getLongitude()我有 Null 值。 The same sometimes happens in the main activity with getUser_Rank and getUsername or other values.在使用getUser_RankgetUsername或其他值的主要活动中有时也会发生同样的情况。 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.在 map 上,重新打开后,我从 SharedPref 获取数据,并且 map 相机移动到我需要的位置。

PS I add data like this PS我添加这样的数据

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.我的英语说得不好,而且很难找到信息。

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);替换这个context.getSharedPreferences(SHARED, MODE_PRIVATE);

with this PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());使用此PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

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

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.将坐标转移到一个单独的方法,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM