Android: shared preferences gives unknown text

TryMySkills :

So i need my app to save and remember the name that user enters in a search field. I use shared preferences to do that.. But when i close the app and open again, instead of the saved name i get a text like this: android.support.AppCompatEditText{1f4bcb VEFD..Cl .F...197,800-860,927 app:id/username}

What am doing wrong?

  static SharedPreferences sharedPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
  namefield = (EditText) findViewById(R.id.username); //this is the user-input field
  Tname = (TextView) findViewById(R.id.NameLabel); //this label will be the username when user press the UPDATEbtn
  UPDATEbtn = (Button)findViewById(R.id.Update_btn);

  // Get from the SharedPreferences
    sharedPref = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
    String USERname = sharedPref.getString("name", String.valueOf(0));
    Tname.setText(USERname);
}




//this is the onClick method of my UPDATEbtn
public void UpdateInfo(View view)
{

Tname.setText(namefield.getText());

    sharedPref = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("name", String.valueOf(namefield));

  // Apply the edits!
    editor.apply();


}
Vinay John :

You are trying to access the edittext.

Try

editor.putString("name", namefield.getText().toString());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291123&siteId=1