Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

Condition is not working in sharedpreferences android

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...