0

I cannot save and Retrieve Data using SharedPreferences... this app was working fine before i added database to this ..

i cannot understand what should i pass to 2nd argument of shared Preference . getString("key","value"); .. is value has to same as i passed in putString method ?

here is my code..

public class MainActivity extends Activity {
    TextView lbl;
    EditText uName, uPass;
    String name, pass;
    SharedPreferences sr = getSharedPreferences("mp", MODE_PRIVATE);
    SharedPreferences.Editor et = sr.edit();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lbl = (TextView) findViewById(R.id.My_Label);
        uName = (EditText) findViewById(R.id.userTxt);
        uPass = (EditText) findViewById(R.id.passTxt);

    }

    //Register Method
    public void doRegister(View v) {

        if ((uName.getText().toString().equals("") && ((uPass.getText().toString()).equals(""))))
            Toast.makeText(this, "Enter UserName and Password First to Get Registered", Toast.LENGTH_LONG).show();

        else {
            name = uName.getText().toString();
            pass = uPass.getText().toString();

            //Save the user to the DataBase


            et.putString("Name",name);
            et.putString("Pswd",pass);
            et.commit();

            //Alert user
            Toast.makeText(this, "User Successfully Registered !", Toast.LENGTH_LONG).show();

            //Clear Login and pass field
            uName.setText("");
            uPass.setText("");
        }
    }


    //Login Method
    public void doLogin(View v) {

        if ((uName.getText().toString().equals("") && ((uPass.getText().toString()).equals("")))) {
            Toast.makeText(this, "Enter UserName and Password First", Toast.LENGTH_LONG).show();

        }

        //User Authenticated
        else if ((uName.getText().toString().equals(sr.getString("Name", name)) && ((uPass.getText().toString()).equals(sr.getString("Pswd", pass))))) {

            Intent it = new Intent(this, SecActivity.class);
            it.putExtra("user", name);
            startActivity(it);
        } else if (!((uName.getText().toString().equals(sr.getString("Name",name)) && !((uPass.getText().toString()).equals(sr.getString("Pswd",pass))))))
            Toast.makeText(this, "Wrong UserName or Password !", Toast.LENGTH_LONG).show();

        //Clears input
        uName.setText("");
        uPass.setText("");

    }

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Haseeb Mir
  • 928
  • 1
  • 13
  • 22
  • where do you retrieve text from shared preferences? – Kostas Drak Apr 30 '16 at 15:26
  • @helldawg13 here : if ((uName.getText().toString().equals(sr.getString("Name",name) – Haseeb Mir Apr 30 '16 at 15:33
  • and by the way. Storing just plaintext passwords in a preferences file is just a sick joke. use SQLLite DB and password encryption for better results. Anyone with rooted device can see the password you are saving... – Kostas Drak Apr 30 '16 at 15:39

1 Answers1

1

I would suggest you to change your code to this:

private SharedPreferences sr;
private SharedPreferences.Editor et;

Save the values to sharedPrefs.

public void doRegister(View v) {

        if ((uName.getText().toString().equals("") && ((uPass.getText().toString()).equals(""))))
            Toast.makeText(this, "Enter UserName and Password First to Get Registered", Toast.LENGTH_LONG).show();

        else {
            name = uName.getText().toString();
            pass = uPass.getText().toString();

            //Save the user to the DataBase

            sr = getSharedPreferences("mp", MODE_PRIVATE);
            sr.edit();
            et.putString("Name",name);
            et.putString("Pswd",pass);
            et.apply();

            //Alert user
            Toast.makeText(this, "User Successfully Registered !", Toast.LENGTH_LONG).show();

            //Clear Login and pass field
            uName.setText("");
            uPass.setText("");
        }
    }

Retrieve Values from sharedPrefs:

//Login Method
    public void doLogin(View v) {
        sr = getSharedPreferences("mp", MODE_PRIVATE)
        //You should put a default value in case the key does not 
        //exist so in our case an empty String.
        String nameFromPrefs = sr.getString("Name", "");
        String passFromPrefs = sr.getString("Pswd", "");

        if ((uName.getText().toString().equals("") && ((uPass.getText().toString()).equals("")))) {
            Toast.makeText(this, "Enter UserName and Password First", Toast.LENGTH_LONG).show();

        }

        //User Authenticated
        else if (uName.getText().toString().equals(nameFromPrefs) && (uPass.getText().toString().equals(passFromPrefs)) {

            Intent it = new Intent(this, SecActivity.class);
            it.putExtra("user", name);
            startActivity(it);
        } else if (!uName.getText().toString().equals(nameFromPrefs) && !uPass.getText().toString().equals(passFromPrefs))
            Toast.makeText(this, "Wrong UserName or Password !", Toast.LENGTH_LONG).show();

        //Clears input
        uName.setText("");
        uPass.setText("");

    }

Hope it helps!!!

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60