Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中从SharedReferences检索int值_Android_Sharedpreferences - Fatal编程技术网

如何在Android中从SharedReferences检索int值

如何在Android中从SharedReferences检索int值,android,sharedpreferences,Android,Sharedpreferences,我试图从SharedReference中保存和获取价值,但就存储数据而言,它工作正常,但在检索价值时我无能为力 //定常 private static final String WALLPAPER_PREF_FILE = "wallpaper"; //用于存储键值对的方法 public void saveWallpaper(Context context,int wallpaperId) { SharedPreferences pref = context.getSharedP

我试图从SharedReference中保存和获取价值,但就存储数据而言,它工作正常,但在检索价值时我无能为力

//定常

private static final String WALLPAPER_PREF_FILE = "wallpaper";
//用于存储键值对的方法

public void saveWallpaper(Context context,int wallpaperId) {

       SharedPreferences pref = context.getSharedPreferences(WALLPAPER_PREF_FILE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor  editor = pref.edit();
        editor.putInt("wallpaperId", wallpaperId);
        editor.commit();
     }
//方法检索存储在SharedReference中的值

 public static int getSavedWallpaper(Context context) {

        SharedPreferences pref = context.getSharedPreferences(
                WALLPAPER_PREF_FILE, Activity.MODE_PRIVATE);
        return pref.getInt("wallpaperId", -1);
    }

看起来你在检索中输入了错别字-wallpaerId。把它修好,它就会工作。(更好的是,在保存和检索时都使用常量字符串,以避免出现此问题)。

尝试这些方法以接收值

public static String getSharedPrefData(Context activity, String key) {
    SharedPreferences prefs = activity.getSharedPreferences(PREFRENCE_NAME,
            Context.MODE_PRIVATE);
    String value = "";
    if (prefs != null && prefs.contains(key)) {
        value = prefs.getInt(key, defaultValue);
    }
    return value;
}
Getter-Setter方法

public void setUserId(int userId) {
    this.userId = userId;
    SharedPreferences.Editor Editor = pref.edit();
    Editor.putInt(PUSH_USER_ID, userId);
    Editor.apply();
    Log.e(TAG, userId + " successfull");
}

public int getUserId() {
    return userId;
}
//检索整数值

SharedPreferences pref = new SharedPreferences();
    pref.getUserId()

步骤1:创建名为SessionManager的公共类

public class SessionManager {

  final static String appPrefNames="MyAppInfo";
    public static SharedPreferences myPrefsSession;
    public static SharedPreferences.Editor prefsEditorSession;

    public static void setUserId(Context context, String userId) {

        myPrefsSession = context.getSharedPreferences(appPrefNames, Context.MODE_PRIVATE);
        prefsEditorSesstion = myPrefsSession.edit();
        try {
           prefsEditorSesstion.putString("user_id", userId);

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


    public static String getUserId(Context c) {
        myPrefsSession = c.getSharedPreferences(appPrefNames, Context.MODE_PRIVATE);
        String userId= myPrefsSession.getString("user_id", "");
        return userId;
    }
}
第2步:在活动课中

SessionManager sessionManager = new SessionManager();
//Setting UserId by method
sessionManager.setUserId(this,"12");

// get Your user id
String userId = sessionManager.getUserId();

用于使用常量+1共享首选项getPreff=PreferenceManager.GetDefaultSharedReferences(getActivity());intresid=getPreff.getInt(“wallpaerId”,即“);只要解决你的拼写错误“wallpaerId”,它就会很好地工作。检查我下面的回答。@Thommy它不工作,尽管我喜欢墙纸,但当“活动”重新创建时,我还是得到了默认墙纸。@手动检查我的更新答案,现在它将帮助您。感谢Suhas先生的及时帮助,但我再次无法检索id。这样,您永远不会得到id:int resId=res.getIdentifier(“墙纸”,getResources().getDrawable(R.drawable.chat\u composer).toString(),this.getPackageName();所以更新代码,如上所示。
public class SessionManager {

  final static String appPrefNames="MyAppInfo";
    public static SharedPreferences myPrefsSession;
    public static SharedPreferences.Editor prefsEditorSession;

    public static void setUserId(Context context, String userId) {

        myPrefsSession = context.getSharedPreferences(appPrefNames, Context.MODE_PRIVATE);
        prefsEditorSesstion = myPrefsSession.edit();
        try {
           prefsEditorSesstion.putString("user_id", userId);

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


    public static String getUserId(Context c) {
        myPrefsSession = c.getSharedPreferences(appPrefNames, Context.MODE_PRIVATE);
        String userId= myPrefsSession.getString("user_id", "");
        return userId;
    }
}
SessionManager sessionManager = new SessionManager();
//Setting UserId by method
sessionManager.setUserId(this,"12");

// get Your user id
String userId = sessionManager.getUserId();