Hello I want to know how do i call a session from android
following is the code for my login page where is set the sessions
Runnable runnable = new Runnable() {
public void run() {
Users user = mapper.load(Users.class,username);
// System.out.println(user.getPassword());
// System.out.println(username);
try {
if (user != null && user.getPassword().equals(password)) {
//System.out.println("Correct");
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("userSession", username);
editor.commit();
runOnUiThread(new Runnable() {
@Override
public void run() {
//setContentView(R.layout.activity_account);
startActivity(new Intent(startActivity.this, accountActivity.class));
}
});
and after logging in it goes to the account page and the code for accound page is shown below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
/* SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
String User=pref.getString("userSession", "Not Exist");
System.out.println("Hello");
System.out.println(User);
*/
test = (Button) findViewById(R.id.streamingButton);
testId = (TextView)findViewById(R.id.welcomeTxt);
}
Now i want to know how do i set the textview to the logged in users username I have commented the sharedpreference code because it does not print the user id aswell. I dont understand why the commented code does not work
You needn't use SharedPreferences, just put data into intent. Then you can get data form onCreate of accountActivity. See this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
Intent intent = getIntent();
String userName = intent.getStringExtra("userName");
test = (Button) findViewById(R.id.streamingButton);
testId = (TextView)findViewById(R.id.welcomeTxt);
}
You can change the value of a text view easily using the setText() method. Or you can add text to the end (without overwriting the existing value) with append().
testId.setText("my value");
I'm not too sure about why the SharedPreferences doesn't work. Does the code for getting the application context work from the Runnable object? Do you need to pass a context to the Runnable, perhaps?
Please use apply in place of commit where you are saving data in shared preference. Please remove only line SharedPreferences.Editor editor = pref.edit(); and uncomment your code. This will give you correct value store in shared preference.
Edit the account page
to this!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
if(pref.contains("userSession") {
String user = pref.getString("userSession", "UNDEFINED");
textview.setText(user);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.