How to save data using Shared Preference

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

How to save data using Shared Preference

Postby Vuvi33 » Wed Nov 10, 2010 8:03 am

Hi all,

I'm new to android and Java and am currently working on an android project. I'm trying to save Name and Number using "EditText" and Shared Preference. I tried typing some codes down, but it doesn't seems to work.

Code: Select all
   protected void loadPreferences(){
      
      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
      Editor e = sp.edit();
      e.putBoolean(PREFS_SETTINGS, true);
      
      e.commit();
   }

   protected void saveActivityPreferences(){

      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
      
      sp.getBoolean(PREFS_SETTINGS, true);
      
   }
   
   


When I select the Save button. It cannot seems to save the Name.

Thank you. Greatly appreciate.
Vuvi33
Freshman
Freshman
 
Posts: 3
Joined: Wed Nov 10, 2010 7:47 am

Top

Re: How to save data using Shared Preference

Postby urbantrad » Wed Nov 10, 2010 10:42 pm

What you do here is only putting and setting a boolean, and nothing besides that. What i think you want to do is save/read the name and number. That would look something like this (similar methods for number):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void saveName(String name) {
  2.       SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
  3.       Editor e = sp.edit();
  4.       e.putString("NAME", name);
  5.       e.commit();
  6. }
  7.  
  8. public String getName() {
  9.       SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
  10.       return sp.getString("NAME", "No name specified");
  11. }
  12.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
:
urbantrad
Senior Developer
Senior Developer
 
Posts: 104
Joined: Thu Sep 09, 2010 10:19 pm

Re: How to save data using Shared Preference

Postby Vuvi33 » Tue Nov 16, 2010 9:23 am

Thank you very much for the help.

Another question please.

Code: Select all
public class Settings extends Activity implements OnClickListener{
   
   Button b3, b4;

   public static final String PREFS_SETTINGS = "mySharedPreferences";
   
--Ommitted --
         
         @Override
         public void onClick(View v) {      
            
            int mode = Activity.MODE_PRIVATE;
            //SharedPreferences mySharedPreferences = getSharedPreferences(PREFS_SETTINGS,mode);
            
              //SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
            
            //Intent data = new Intent();
              EditText na1 = (EditText) findViewById(R.id.txt_Person1);
              na1.setText(PREFS_SETTINGS, null);
              EditText na2 = (EditText) findViewById(R.id.txt_Person2);
              na2.setText(PREFS_SETTINGS, null);



            showDialog(0);
            
         }
      });
   
    }
   



When I typed "Tim" in the textbox and select the "Save" button, the text in the textbox is chnaged to "mySharedPreferences". Can I know where my mistake is and how to correct it.

and

May I ask how can I save my data using the codes below.

Code: Select all
            int mode = Activity.MODE_PRIVATE;
            //SharedPreferences mySharedPreferences = getSharedPreferences(PREFS_SETTINGS,mode);
            
              //SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);



I'm creating a xml files after saving. But I don't how to.

Thank you. Help greatly appreciate. :)
Vuvi33
Freshman
Freshman
 
Posts: 3
Joined: Wed Nov 10, 2010 7:47 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests