【问题标题】:Why sharedpreference value not getting in when retrieving?为什么在检索时共享偏好值没有进入?
【发布时间】:2016-08-08 11:29:21
【问题描述】:

我添加了三个共享首选项,如下代码所示。而且我能够仅检索共享偏好值。

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("Loggedin",true);
            editor.putString("userId",userid);
            editor.putString("pwd",password);
            editor.apply();
            editor.commit();

我使用以下代码从另一个活动中检索。我只能检索布尔值。其他值不存在。获取字符串值的默认值。请帮帮我。

   SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

                Boolean loggedin=preferences.getBoolean("Loggedin", false);
                String userId=preferences.getString("userId", "0");
                String pwd=preferences.getString("pwd", "0");

【问题讨论】:

  • 您是否获得了其他活动的价值?
  • 布尔登录=preferences.getBoolean("Loggedin", false);如何产生偏好
  • @Singh 是的。我正在检索另一个活动的值
  • 发布完整代码,您在尝试从中检索值时如何实例化您的偏好。
  • 你为什么同时使用apply()commit()?!

标签: android sharedpreferences


【解决方案1】:

首先检查您存储在首选项中的值是否存储 使用此代码

Boolean loggedin=preferences.getBoolean("Loggedin", false);
 String userId=preferences.getString("userId", null);
 String pwd=preferences.getString("pwd", null);


if(userId==null || pwd==null)
{
//data not therer
}
else
{
//do something with data
}

如果发生任何错误,请告诉我..

【讨论】:

    【解决方案2】:

    我认为您没有以正确的方式获取SharedPreferences。 参见文档,例如:https://developer.android.com/training/basics/data-storage/shared-preferences.html

    Context context = getActivity();
    SharedPreferences sharedPref = context.getSharedPreferences(
        getString(R.string.preference_file_key), Context.MODE_PRIVATE);
    

    此外,您无需致电apply()commit()。只要其中一个就足够了。请参阅 javadoc 了解它们之间的差异。

    【讨论】:

      【解决方案3】:

      使用此代码

      String userId=preferences.getString("userId", null);
                      String pwd=preferences.getString("pwd", null);
      

      【讨论】:

        【解决方案4】:
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        Boolean loggedin=preferences.getBoolean("Loggedin", false);
                    String userId=preferences.getString("userId", "");
                    String pwd=preferences.getString("pwd", "");
        
        if(userId==null || userId==""||pwd==null ||pwd=="")
        {
        
        }
        else
        {
        
        }
        

        【讨论】:

          【解决方案5】:

          以这种方式尝试代码。

          在第一个活动中设置值

           SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
              SharedPreferences.Editor editor = preferences.edit();
              editor.putBoolean("Loggedin",true);
              editor.putString("userId",userid);
              editor.putString("pwd",password);
              editor.apply();
          

          在第二个活动中检索值

           SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
          
                      Boolean loggedin=preferences.getBoolean("Loggedin", false);
                      String userId=preferences.getString("userId", "");
                      String pwd=preferences.getString("pwd", "");
          

          【讨论】:

            【解决方案6】:

            以这种方式用于从代码的共享首选项中检索值。

            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            
                        Boolean loggedin=preferences.getBoolean("Loggedin", false);
            //Checking the value of userId and pwd,if they are null then there is no values of userId and pwd other than default.
                         if (userId != null && pwd != null) {
                    String userId = preferences.getString("userId", "0");
                    String pwd = preferences.getString("pwd", "0");
                } else {
                    String userId = "0";
                    String pwd = "0";
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-12-30
              • 2013-09-10
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多