The SharedPreferences doesn't save my username while I don't get any error. I have checked my method with some tutorials and I can't find what's wrong. I have tried both commit()
and 'apply()' despite that can't make here a difference.
Someone who finds the error?
my code:
public class MainActivity extends AppCompatActivity implements AsyncResponse, View.OnClickListener {
EditText etUsername, etPassword;
ImageButton btnLogin;
String username;
CheckBox ckbx_login;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
btnLogin = (ImageButton) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
username = etUsername.getText().toString();
}
@Override
public void processFinish(String result) {
if(result.equals("success")) {
SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(PREFS_NAME,0).edit();
editor.putString("loginname",username);
editor.commit();
Intent in = new Intent(this, SubActivity.class);
String naam2= etUsername.getText().toString();
in.putExtra("naam2",naam2);
startActivity(in);
}
else{
Toast.makeText(this,"Inloggen mislukt", Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View view) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString());
PostResponseAsyncTask task = new PostResponseAsyncTask(this, postData);
task.execute("http://exemple.com");
}
And the second class:
public static final String PREFS_NAME = "MyPrefsFile";
String name;
TextView msg_welcome;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_sub);
msg_welcome = (TextView) findViewById(R.id.txtVwelcome);
SharedPreferences prefs = this.getSharedPreferences(PREFS_NAME,0);
String restored_loginname = prefs.getString("loginname",null);
if(restored_loginname != null) {
name = prefs.getString("name", "No name defined");
}
Toast.makeText(getApplicationContext(),name,Toast.LENGTH_LONG).show();
}
public void scan(View View){
zXingScannerView = new ZXingScannerView(getApplicationContext());
setContentView(zXingScannerView);
zXingScannerView.setResultHandler(this);
zXingScannerView.startCamera();
}
@Override
protected void onPause() {
super.onPause();
zXingScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
// Toast.makeText(getApplicationContext(),result.getText(),Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), Avt_form2.class);
String qrcode = result.getText();
i.putExtra("qrcode",qrcode);
startActivity(i);
}
Btw I know that Postdata is outdated. I am looking to replace it.
Issue : currently your taking the value of username
in onCreate
when you don't have the user input so
username = etUsername.getText().toString();
should be executed after user input is available like in onclick
or
@Override
public void processFinish(String result) {
if(result.equals("success")) {
SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(PREFS_NAME,0).edit();
username = etUsername.getText().toString();
//^^^^^^^^^
editor.putString("loginname",username);
editor.commit();
Note : name
was not previously used to store any value , i guess wanted to use name2
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.