无法从Android中的SharedPreferences保存和检索数据

时间:2016-04-30 15:23:05

标签: android sharedpreferences

我无法使用SharedPreferences保存和检索数据... 这个应用程序在我添加数据库之前工作正常..

我无法理解我应该传递给共享偏好的第二个参数。的GetString("键""值&#34); ..是否与我在putString方法中传递的值相同?

这是我的代码..

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

    }

}

1 个答案:

答案 0 :(得分:1)

我建议您将代码更改为:

private SharedPreferences sr;
private SharedPreferences.Editor et;

将值保存到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("");
        }
    }

从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("");

    }

希望它有所帮助!!!