-3

Not getting value from sharedpreference to textview. Look into code. Is there any mistake. And what is the difference in between the following two lines of code...

sharedPreferences = getSharedPreferences("Mypreferences",Context.MODE_PRIVATE);

and

sharedPreferences=PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

The following is my lines of code..

public class MainActivity extends AppCompatActivity {
  EditText et_name, et_phon, et_email, et_city;
  Button button_submit, button_show;
  SharedPreferences sharedPreferences;
  SharedPreferences.Editor editor;
  TextView tv_name,tv_phone,tv_email,tv_city;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initComponents();
    sharedPreferences = getSharedPreferences("Mypreferences", Context.MODE_PRIVATE);

    button_submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name, phon, email, city;
            name = et_name.getText().toString().trim();
            phon = et_phon.getText().toString().trim();
            email = et_email.getText().toString().trim();
            city = et_city.getText().toString().trim();

            editor = sharedPreferences.edit();
            editor.putString("NameKey", name);
            editor.putString("phoneKey", phon);
            editor.putString("emailKey", email);
            editor.putString("cityKey", city);
            editor.apply();
            Toast.makeText(MainActivity.this, "Submitted", Toast.LENGTH_SHORT).show();


        }
    });
    button_show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            String n = sharedPreferences.getString("NameKey", null);
            String p = sharedPreferences.getString("phoneKey", null);
            String e = sharedPreferences.getString("emailKey", null);
            String c = sharedPreferences.getString("cityKey", null);

            tv_name.setText(n);
            tv_phone.setText(p);
            tv_email.setText(e);
            tv_city.setText(c);

            Toast.makeText(MainActivity.this, "Show button clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

private void initComponents() {

    et_name = (EditText) findViewById(R.id.et_name_main);
    et_phon = (EditText) findViewById(R.id.et_phone_main);
    et_email = (EditText) findViewById(R.id.et_email_main);
    et_city = (EditText) findViewById(R.id.et_city_main);
    tv_name = (TextView) findViewById(R.id.tv_name_main);
    tv_phone = (TextView) findViewById(R.id.tv_phone_main);
    tv_email = (TextView) findViewById(R.id.tv_email_main);
    tv_city = (TextView) findViewById(R.id.tv_city_main);
    button_submit = (Button) findViewById(R.id.btn_submit_main);
    button_show = (Button) findViewById(R.id.btn_view_main);

}
}

Any type of help is appreciated.

Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
  • this simply creates two different sharedPreferences.xml . The default is named shared_preferences in your app directoy, the other one is named with "mypreferences" in your case. MODE_PRIVATE means, no other app can enter it. – Opiatefuchs Apr 11 '16 at 06:55
  • 2
    You are saving in different instance of SharedPreferences & retrieving in default one. Use `getSharedPreferences("Mypreferences", Context.MODE_PRIVATE)` or default one both while putting & retrieving. – VenomVendor Apr 11 '16 at 06:55
  • Thanxx. U guys cleared my point i want to know about MODES in that argument . – Harsh Bhavsar Apr 11 '16 at 07:12

2 Answers2

1

Problem is, you are creating one sharedPref called "Mypreferences" to store values and trying to retrieve values from default sharedPreferences which does not hold the values you have put.

Change the line

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

to

SharedPreferences sharedPreferences = getSharedPreferences("Mypreferences", Context.MODE_PRIVATE); 
Apurva
  • 7,871
  • 7
  • 40
  • 59
0

You are creating two sharedPreferences xml files in Your app directory. The default one is the standard preferences xml, the other one which you have created with

sharedPreferences = getSharedPreferences("Mypreferences", Context.MODE_PRIVATE);

will store your prefs inside a Mypreferences.xml. MODE_PRIVATE means, that this preferences are only available for your app (but on a rooted device, everybody can access).

If you want to access your saved sharedPreferences, you have to use the same preferences that you have used to save them. That means either you have to use everywhere you are accessing the same keys:

sharedPreferences = getSharedPreferences("Mypreferences", Context.MODE_PRIVATE);

or

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

not both. In Your button_show click method, you don´t have to create a new sharedPreferences object, just delete this line:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

because you have generated a global sharedPreferences object and can access it everywhere in your class.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49