Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using shared preferences editor

Tags:

I'm slowly working through an Android learning book and was given the following code to assign user data:

package com.androidbook.triviaquiz;  import android.app.Activity; import android.content.SharedPreferences;  public class QuizActivity extends Activity {     public static final String GAME_PREFERENCES = "GamePrefs";     SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);     SharedPreferences.Editor prefEditor = settings.edit();     prefeditor.putString("UserName", "John Doe"); //**syntax error on tokens**     prefEditor.putInt("UserAge", 22); //**syntax error on tokens**     prefEditor.commit(); } 

However, I get an error (lines indicated with comments) that underlines the period and says "misplaced construct" and also that underlines the arguments saying "delete these tokens". I have seen this done in other applications in the same format, I don't understand what is wrong.

like image 858
Gaege Avatar asked Feb 11 '11 09:02

Gaege


People also ask

How do I edit shared preferences?

Mark in the editor to remove all values from the preferences. Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. Set a boolean value in the preferences editor, to be written back once commit() or apply() are called.

What are shared preferences and how do you use it?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

What does the SharedPreferences editor Clear () method do?

clear(), It delete only values (means that keys are available) or it delete key value pair. check the following code. Mark in the editor to remove all values from the preferences.

Why do we need shared preferences?

Shared preferences allow you to store small amounts of primitive data as key/value pairs in a file on the device. To get a handle to a preference file, and to read, write, and manage preference data, use the SharedPreferences class. The Android framework manages the shared preferences file itself.


1 Answers

Edit: Of course! Those statements cannot be put directly into the class at that level and must be inside a method, something like this:

public class QuizActivity extends Activity {     public static final String GAME_PREFERENCES = "GamePrefs";      @Override     public void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);         SharedPreferences.Editor prefEditor = settings.edit();         prefEditor.putString("UserName", "John Doe");         prefEditor.putInt("UserAge", 22);         prefEditor.putString("Gender", "Male");         prefEditor.commit();     } } 
like image 112
trojanfoe Avatar answered Nov 12 '22 05:11

trojanfoe