-2

i am trying to create an app that would reduce paper usage in school so i need the teacher to write his name in the settings activity and then his name to show up in the main activity this is my activity with textview to show the name

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String key = null; 
    setContentView(R.layout.activity_main);
    SharedPreferences sp = getSharedPreferences(key, Activity.MODE_PRIVATE);
    String name = sp.getString(key, "");
    TextView name2 = (TextView) findViewById(R.id.textView1);
    name2.setText(name);
}
public void onResume(View view) {
    String key = null; 
    SharedPreferences sp = getSharedPreferences(key, Activity.MODE_PRIVATE);
    String name = sp.getString(key, "");
    TextView name2 = (TextView) findViewById(R.id.textView1);
    name2.setText(name);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void save(View view){
    Intent intent = new Intent(this, MainActivity2.class);
    startActivity(intent);
}

and this is the activity where to enter name to be saved:

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_activity2, menu);
    return true;
}
public void save2 (View view) {
    final String key = "placeKey";  
    EditText save = (EditText) findViewById(R.id.editText1);
    String save2 = save.getText().toString(); 
    SharedPreferences sp = getSharedPreferences(key, Activity.MODE_PRIVATE);
    SharedPreferences.Editor edt = sp.edit();
    edt.putString(key, save2);
    edt.commit();

    String name = sp.getString(key, "");
    TextView name2 = (TextView) findViewById(R.id.textView1);
    name2.setText(name);
}
public void hrag (View view){
    final String key = "placeKey"; 

       SharedPreferences sharedpreferences;
    SharedPreferences sp = getSharedPreferences(key, Activity.MODE_PRIVATE);
    String name = sp.getString(key, "");
    TextView name2 = (TextView) findViewById(R.id.textView1);
    name2.setText(name);

}

any help would be great

  • 3
    See here http://stackoverflow.com/a/12074219/940834 .. This question has been asked so many times. Try using google next time. – IAmGroot Feb 19 '14 at 15:05

1 Answers1

1

To save into SharedPreferences, create SharedPreferences instance by passing Context of the activity...as follows...

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putString("key", value);
prefsEditor.commit();

Retrieve from SharedPreferences as follows...

prefsEditor.getString("key", "");
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41