Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Android
SharedPreference not working
shawn peter
Ranch Hand
Posts: 1325
1
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
this is my main class
package com.virtual.yourlocation; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class SharedVariables extends Activity implements OnClickListener{ //private SharedPreferences sharedPreferences; private EditText txt1; private TextView textview; public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.shared); txt1=(EditText)findViewById(R.id.SharedText1); textview=(TextView)findViewById(R.id.textView1); Button enetr=(Button)findViewById(R.id.SharedSave); enetr.setOnClickListener(this); Button load=(Button)findViewById(R.id.SharedLoad); load.setOnClickListener(this); } @Override public void onClick(View v) { String name=txt1.getText().toString(); if(v.getId()==R.id.SharedSave){ SavePreferences("name",name); Toast.makeText(this, "SAVEEEEEEEEEE", 3); } if(v.getId()==R.id.SharedLoad){ Toast.makeText(this, "LOADDDDDDDDDDD", 3); LoadPreference(); } //SavePreferences("Name", name); //LoadPreference(); } private void SavePreferences(String key, String value){ SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(key, value); editor.commit(); } private void LoadPreference(){ SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); String strSavedMem1 = sharedPreferences.getString("name", ""); textview.setText(strSavedMem1); Toast.makeText(this, "LOADDDDDDDDDDD", 3); } }
When i run the programm it is working fine.But when i stop the programm and again run it doesn't show me saved value for sharedpreference ?
where is the error ?
Hardik Trivedi
Ranch Hand
Posts: 252
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You need to create shared preference in a different way...
http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html
Try tutorial from above link.
Or attach source code. I will correct it.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How to retrieve string from EditText?
Creating Database and Tables
how to add a google search option
how to use distanceBetween built in methog
Cannot find the problem in the code
More...