SharedPreferences 用于显示文本,不显示

SharedPreferences used to display text, not showing

我正在尝试在我的导航抽屉中设置用户文本。对于名称和课程这两个值,我从数据库 class 调用字符串方法并将其存储在字符串中。另外两个值 id 和 email 我从用户那里获取并存储它。我使用 SharedPreferences 来传递值。然后在我的导航 activity 里面 onCreate 我得到值然后将它设置为适当的文本视图。但是什么也没有出现。注册后它仍然是空的。我在这里做错了什么?

我还应该提一下,我在注册后不会开始导航 activity。它是注册->登录->仪表板->导航。所以它是我传递值的第 4 个 activity。 我已经使用 Google 提供的导航 activity 并在 onCreate 中创建了 settext,仍然没有得到所需的输出。

导航:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        txtName = (TextView) findViewById(R.id.n);
        txtsid = (TextView) findViewById(R.id.s);
        txtcourse = (TextView) findViewById(R.id.c);
        txtemail = (TextView) findViewById(R.id.e);

        SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);

            String name = myPrefs.getString("name", "null");
            String course = myPrefs.getString("course", "null");
            String sid = myPrefs.getString("sid", "null");
            String email = myPrefs.getString("email", "null");

            if (myPrefs.contains("name")) txtName.setText(name);
            if (myPrefs.contains("course")) txtcourse.setText(course);
            if (myPrefs.contains("sid")) txtsid.setText(sid);
            if (myPrefs.contains("email")) txtemail.setText(email);


    }

注册:

buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (validate()) {
                    String Email = editTextEmail.getText().toString();
                    String SID = editTextSID.getText().toString();
                    String Password = editTextPassword.getText().toString();
                    progressBar.setVisibility(View.VISIBLE);

                    //Check in the database is there any user associated with  this email

                    if (!sqliteHelper.ifUserRegistered(SID) && sqliteHelper.ifUserExists(SID) && !sqliteHelper.isEmailExists(Email)) {

                        sqliteHelper.registerUser(new User(null, Email, SID, Password));
                        Snackbar.make(buttonRegister, "User created successfully! Please Login ", Snackbar.LENGTH_LONG).show();

                        String name = sqliteHelper.getUserNameFromSID(SID);
                        String course = sqliteHelper.getUserCourseFromSID(SID);
                        SharedPreferences myPrefs = getSharedPreferences("SharedPref", MODE_PRIVATE);
                        SharedPreferences.Editor editor = myPrefs.edit();
                        editor.putString("name", name);
                        editor.putString("course", course);
                        editor.putString("sid", SID);
                        editor.putString("email", Email);
                        editor.commit();

                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finish();
                            }
                        }, Snackbar.LENGTH_LONG);

我的文本视图在 nav_header.xml 下,只有布局高度和宽度属性,这包含在 headerlayout 属性下的主要 navigation.xml 中。

尝试替换您的代码,

txtName = (TextView) findViewById(R.id.n);
        txtsid = (TextView) findViewById(R.id.s);
        txtcourse = (TextView) findViewById(R.id.c);
        txtemail = (TextView) findViewById(R.id.e);

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
View headerView = navigationView.getHeaderView(0); 
txtName  = (TextView)  headerView.findViewById(R.id.n);
txtsid = (TextView) headerView.findViewById(R.id.s);
txtcourse = (TextView) headerView.findViewById(R.id.c);
txtemail = (TextView) headerView.findViewById(R.id.e);

您的问题是由错误的文件名引起的。

在您的 Register 中,您使用了 getSharedPreferences("SharedPref", MODE_PRIVATE); 基本上,您的 SharedPreference 文件被命名为 SharedPref

但是,在 Navigation 中,您使用了 getSharedPreferences("myPrefs", MODE_PRIVATE); 基本上,您正在获取一个名为 myPrefs 的 SharedPreference 文件。

所以您只是保存到一个文件,但试图从另一个文件中获取该数据。

因此,在您的 Navigation 中,当您以这种方式编码 setText() 时:

if (myPrefs.contains("name")) txtName.setText(name);
if (myPrefs.contains("course")) txtcourse.setText(course);
if (myPrefs.contains("sid")) txtsid.setText(sid);
if (myPrefs.contains("email")) txtemail.setText(email);

myPrefs 文件不可能包含这些名称,因此它总是无法通过检查并且永远不会调用 setText()

所以解决这个问题的方法就是使用相同的文件名。

导航中替换:

SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);

与:

SharedPreferences myPrefs = getSharedPreferences("SharedPref", MODE_PRIVATE);