条件在sharedpreferences android中不起作用 [英] Condition is not working in sharedpreferences android
问题描述
我有一个登录页面,因此我使用了 SharedPreferences
.我正在尝试保存值.没有值,它不应该转到另一个 Activity
因为我在登录页面中给出了这个:
I have a Login page, so for that I am using SharedPreferences
. I am trying to save values.
Without values, it shouldn't go to the another Activity
for that I have given this in Login page:
if((spf != null) && (spv !=null) && (et1v != null) && (et2v != null) && (et3v != null) &&
(sp != null) && (et1 != null) && (et2 != null) && (et3 != null)){
finish();
Intent i = new Intent(Login.this,Welcome.class);
startActivity(i);
}
else{
btn1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sp.getSelectedItem().toString().length() > 0 &&
et1.getText().toString().length() > 0 &&
et2.getText().toString().length() > 0 &&
et3.getText().toString().length() > 0 )
{
SharedPreferences spf = getSharedPreferences("newprfs", Context.MODE_PRIVATE);
SharedPreferences.Editor spe = spf.edit();
spe.putString("uname",sp.getSelectedItem().toString());
spe.putString("password",et1.getText().toString());
spe.putString("mobile",et2.getText().toString());
spe.putString("dept",et3.getText().toString());
spe.commit();
finish();
Intent i = new Intent(Login.this,Welcome.class);
startActivity(i);
}
}
}
);
}
这是我的欢迎页面
SharedPreferences spf = getSharedPreferences("newprfs", Context.MODE_PRIVATE);
SharedPreferences.Editor spe = spf.edit();
String spv = spf.getString("uname","");
String et1v = spf.getString("password","");
String et2v = spf.getString("mobile","");
String et3v = spf.getString("dept","");
if((spe != null) && (spv !=null) && (et1v != null) && (et2v != null) && (et3v != null) &&
(sp != null) && (et1 != null) && (et2 != null) && (et3 != null)){
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Intent i = new Intent(Welcome.this,MainActivity.class);
startActivity(i);
Welcome.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
else{
Intent i = new Intent(Welcome.this,Login.class);
startActivity(i);
Welcome.this.finish();
}
}
问题是它总是显示一个空白屏幕.我试图检查 Log.d("@@@@@@@@@@@@@@Loginpage@@@@@@@@@@@@@@@@")
;所以我认识到它总是在登录页面加载并且 if condition
不起作用..有人可以告诉我 if Condition..
有什么问题吗?
The problem is that it always showing a blank screen. I tried to check with Log.d("@@@@@@@@@@@@@@Loginpage@@@@@@@@@@@@@@@@")
;
so i recognised to its loading always in Login page and if condition
is not working.. can any one suggest me whats wrong in if Condition..
?
更新这里不是共享首选项的问题......但这里是条件的问题......
Update Here Its not Problem with Shared Prefs.. But here Its the Problem with Condition...
我没有输入值的 If Condition
它不应该移动到下一个......而且如果之前已经定义了值,它应该移动到下一页......
I the If Condition
without Entering Values it should not move to Next... and Also if values already Defined previously it should Move to next page...
所以它移动到下一页和在下一页也发生同样的事情,因为我给出了相同的条件,再次它再次移动到登录页面.所以它停留在单页上,我得到了空白屏幕.
So Its moving to next page and In next page Also Same happening Because I have given same condition and Again Its moving to Login page again. So it stays on the Single page and I am getting blank Screen.
推荐答案
你需要试试 contains(String Key)
试试这个 spv.contains("uname")
而不是 (spv !=null)
或 spv.equals("")
我也遇到过这个问题最好使用 Hashmap ......但这适用于你的案例..
Try this spv.contains("uname")
instead of (spv !=null)
or spv.equals("")
I also Faced this issue Better To use Hashmap... but this works for your Case..
这篇关于条件在sharedpreferences android中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!