//persist HashMap<String, Integer> counters; //the hashmap you want to save SharedPreferences pref = getContext().getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); for (String s : counters.keySet()) { editor.putInteger(s, counters.get(s)); } editor.commit(); //load SharedPreferences pref = getContext().getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE); HashMap<String, Integer> map= (HashMap<String, Integer>) pref.getAll(); for (String s : map.keySet()) { Integer value=map.get(s); //Use Value }
//persist HashMap<String, Integer> counters; //the hashmap you want to save SharedPreferences pref = getContext().getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); for (String s : counters.keySet()) { editor.putInteger(s, counters.get(s)); } editor.commit(); //load SharedPreferences pref = getContext().getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE); HashMap<String, Integer> map= (HashMap<String, Integer>) pref.getAll(); for (String s : map.keySet()) { Integer value=map.get(s); //Use Value }
SharedPreferences preferences = getSharedPreferences("myapp",4); SharedPreferences.Editor editor = preferences.edit(); int points= pointsGame1 + pointsGame2 + pointsGame3 + pointsGame4; editor.putInteger("points", points); editor.commit();
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(ProjectAct.this).edit(); editor.putInteger(StaticVariable.BOOKID, "1"); p.get("contract_no")); editor.commit();
public class CountActivity extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state); . . . SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); int counter= settings.getInteger("counter", 0) +1; SharedPreferences.Editor editor = settings.edit(); editor.putInteger("counter", counter); editor.commit(); }
SharedPreferences settings = getSharedPreferences("settings", 0); SharedPreferences.Editor editor = settings.edit(); editor.putInteger("lastNumber", n); // Commit the edits! editor.commit();
// getting access to SharedPreferences SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE); // reading data Integer yourInteger = prefs.getInteger("your_integer_name", defaultIntegerValue); // persisting data SharedPreferences.Editor editor = mySharedPreferences.edit(); editor.putInteger("your_integer_name", yourInteger); editor.commit();
SharedPreferences preferences = getSharedPreferences("myapp",4); points= preferences.getInteger("points", 0); points= point+pointsGame1; SharedPreferences.Editor editor = preferences.edit(); editor.putInteger("points", points); editor.commit();