无法在片段中获取已保存的共享首选项

我正在开发一个应用程序,在获取我的共享首选项(保存在登录活动中)时,我试图在仪表板片段中获取它,但我无法获取它。在此之后,我检查了是否已保存,然后我使用了


boolean ok= editor.commit();

Toast.makeText(Login.this, "Saved: "+ok, Toast.LENGTH_LONG).show();

我的吐司将消息显示为Saved:true


在这次尝试之后,我假设我的数据已保存到首选项,但我无法获取它。下面是我的仪表板 Fragmenr 代码。


public class dashboard extends Fragment {

private TextView comp_text,mail_text,gst_text;

private String mUsername;

private SharedPreferences mSharedPreferences;




@Nullable

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    //this inflates out tab layout file.

    View x = inflater.inflate(R.layout.dashboard_frag, null);

    comp_text=(TextView)x.findViewById(R.id.company_id);

    mail_text=(TextView)x.findViewById(R.id.email_id);

    gst_text= (TextView)x.findViewById(R.id.gst_id);

    initSharedPreferences();

    Toast.makeText(getActivity(), "Logged member->  "+mUsername, Toast.LENGTH_LONG).show();

    return x;


}

private void initSharedPreferences() {

     mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

    mUsername = mSharedPreferences.getString(Constants.USERNAME, "");


    }

}

这里我的 Toast 显示 **logged member-> **,这意味着 musername 没有任何可打印的内容并且无法获取首选项。


我仍然很困惑,这是我的观点,如果您愿意,我可以显示我保存首选项的位置。


帮助将不胜感激!谢谢 !


编辑 1 ---- 这是我保存首选项的 onResponse 函数。


 public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

            if(response.isSuccessful()) {

                ServerResponse serverResponse = response.body();

                if(serverResponse.getMessage().equals(username)) {

                    SharedPreferences.Editor editor = mSharedPreferences.edit();

                    editor.putBoolean("LoggedIn",true);

                    editor.putString(Constants.USERNAME,serverResponse.getMessage());


                   boolean ok= editor.commit();



                    Toast.makeText(Login.this, "Saved: "+ok, Toast.LENGTH_LONG).show();

                    goToProfile();

                }

            } 



慕哥9229398
浏览 122回答 2
2回答

红颜莎娜

解决方案:这是存储和检索的简单示例 Shared Preferences在首选项中设置值:// MY_PREFS_NAME - a static String variable like:&nbsp;//public static final String MY_PREFS_NAME = "MyPrefsFile";SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();&nbsp;editor.putString("name", "Elena");&nbsp;editor.putInt("idName", 12);&nbsp;editor.commit();从首选项中检索数据:SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);&nbsp;String restoredText = prefs.getString("text", null);if (restoredText != null) {&nbsp; String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.&nbsp; int idName = prefs.getInt("idName", 0); //0 is the default value.}在您的情况下,您可能想要替换您的代码,如下所示:尝试这个SharedPreferences.Editor editor = getSharedPreferences("my_prefs", MODE_PRIVATE).edit();editor.putBoolean("LoggedIn",true);editor.putString(Constants.USERNAME,serverResponse.getMessage());boolean ok= editor.commit();然后在片段中mSharedPreferences = getActivity().getSharedPreferences("prefs", MODE_PRIVATE); ;mUsername = mSharedPreferences.getString(Constants.USERNAME, "");如果要注销并删除用户登录,只需清除SharedPreferences:SharedPreference.Editor pref = context.getSharedPreferences("prefs", MODE_PRIVATE).edit();pref.clear();pref.commit();希望这可以帮助。

有只小跳蛙

尝试这个SharedPreferences.Editor editor = getSharedPreferences("my_prefs", MODE_PRIVATE).edit();;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.putBoolean("LoggedIn",true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.putString(Constants.USERNAME,serverResponse.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean ok= editor.commit();然后在片段中mSharedPreferences = getActivity().getSharedPreferences("my_prefs", MODE_PRIVATE); ;&nbsp; &nbsp; mUsername = mSharedPreferences.getString(Constants.USERNAME, "");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java