Condition is not working in sharedpreferences android

冷暖自知 提交于 2019-12-13 10:53:38

问题


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);
                           }
                       }
                   }
           );
        }

this my Welcome page

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();
        }
    }       

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...

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.


回答1:


You need to try with contains(String Key)

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..



来源:https://stackoverflow.com/questions/46661277/condition-is-not-working-in-sharedpreferences-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!