I am trying to set User text in my Navigation drawer. For two values name and course i call string method from database class and store it in a string. The other two values id and email i get it from the user and store it. I use SharedPreferences to pass the values. Then in my navigation activity inside onCreate i get the values then set it to the appropriate textview. But nothing shows up. It remains empty after registering. What am i doing wrong here?
I should also mention that i don't start navigation activity after registration. It's Register->Login->Dashboard->Navigation. So its the 4th activity to which i am passing the values. I have used the Navigation activity provided by Google and created the settext inside onCreate, still not getting the desired output.
Navigation:
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);
}
Register:
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);
My textviews are under nav_header.xml with just layout height and width attribute, and this is included in the main navigation.xml under the headerlayout attribute.
Your issue is caused by an incorrect filename.
In your Register, you used getSharedPreferences("SharedPref", MODE_PRIVATE);
Basically, your SharedPreference file is named SharedPref.
However, in Navigation, you used getSharedPreferences("myPrefs", MODE_PRIVATE);
Basically, you're fetching a SharedPreference file named myPrefs.
So you're simply saving to one file, but trying to fetch that data from another file.
Therefore, in your Navigation, when you coded your setText()
in this way:
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);
There's no way the myPrefs
file will contain those names, so it'll always fail the check and setText()
is never called.
So the fix for this is simply to use the same filename.
In Navigation replace:
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
with:
SharedPreferences myPrefs = getSharedPreferences("SharedPref", MODE_PRIVATE);