-5

I want to save my String information with sharefPreferences in file as a key-value

Adnan
  • 4,975
  • 5
  • 24
  • 44
Ruben JG
  • 97
  • 7
  • Search in google, lot's example are available for shared preference. – InnocentKiller Feb 27 '14 at 10:59
  • 1
    Use this link for the solution http://stackoverflow.com/a/11027631/2675669 – Nambi Feb 27 '14 at 11:00
  • possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Assaf Gamliel Feb 27 '14 at 11:03
  • You may follow this simple [tutorial](http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html): – Adnan Feb 27 '14 at 11:05

2 Answers2

3

The following code shows how to create and store shared preferences data:

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences.Editor editor = preferences.edit();
  editor.putString("Name","JITESH");
  editor.putString("Password","password1234");
  editor.commit();

The following shows how to retrieve shared preferences data:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String name = preferences.getString("Name","");
  String password = prefs.getString("Password", "");

another practice is shown here accodingly==>

shows how to create and store shared preferences data:

SharedPreferences prefs = getSharedPreferences("myDataStorage",
MODE_PRIVATE);
Editor mEditor = prefs.edit();
mEditor.putString("username","datastorageuser1");
mEditor.putString("password","password1234");
mEditor.commit();

The following shows how to retrieve shared preferences data:

SharedPreferences prefs = getSharedPreferences("myDataStorage",
MODE_PRIVATE);
String username = prefs.getString("username", "");
String password = prefs.getString("password", "");

for more visit my blog

http://upadhyayjiteshandroid.blogspot.in/2013/01/android-working-with-shared-preferences.html

Jitesh Upadhyay
  • 6,557
  • 1
  • 27
  • 44
1

To obtain shared preferences:

SharedPreferences prefs = this.getSharedPreferences(
  "com.example.app", Context.MODE_PRIVATE);

To read preferences:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime());

To edit and save preferences:

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();

See this

Community
  • 1
  • 1
betteroutthanin
  • 6,578
  • 8
  • 27
  • 47