onDataChange
如何在SharedPreferences中存储值?值是纬度和经度。然后我想用片段检索它们,任何想法?
答案 0 :(得分:0)
试试这个,
SharedPreferences preferences = getSharedPreferences("AppPrefs", MODE_PRIVATE);
SharedPreferences.Editor PrefsEditor = preferences.edit();
//set value
Double longitude latitude,
PrefsEditor.putString("latitude", longitude+"");
PrefsEditor.putString("longitude", latitude+"");
//get value
String latitude_value=preferences.getString("latitude", "");
String longitude_value=preferences.getString("longitude", "");
答案 1 :(得分:0)
SharedPreferences preferences = context.getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor PrefsEditor = preferences.edit();
//set value to pref
Double longitude latitude,
PrefsEditor.putString("latitude", longitude+"");
PrefsEditor.putString("longitude", latitude+"");
//get value from pref in fragment
SharedPreferences preferences = getActivity().getSharedPreferences("MyPrefs", MODE_PRIVATE);
String latitude_value=preferences.getString("latitude", "");
String longitude_value=preferences.getString("longitude", "");
prefsEditor.apply()