2016-04-07 11 views
0

私は店で私の共有設定で位置を保存したいし、それを編集して、アプリケーションの開始が、おかげで、私はこれを持っているときにそれを開くことができます。Androidの位置を保存するための共有設定の使い方

//load shared preferecnes 
SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs", 
                  Context.MODE_PRIVATE); 
//note: here idk how to read the last value of position saved 
//Log.v("lastposition", sharedPreferences); 

// on edit preferences and save 
SharedPreferences.Editor editor = sharedpreferences.edit(); 
editor.putString("position", New LatLang(30,40)); 
editor.commit(); 

みんなありがとうは


を:)

LAST EDITのCHANGES:

//load shared preferecnes 
SharedPreferences sharedpreferences = getSharedPreferences(this,Context.MODE_PRIVATE); 

// --> HERE IDK HOT LOAD LAST POSITIONS SAVED 

// on edit preferences and save 
SharedPreferences.Editor editor = sharedpreferences.edit(); 
editor.putDouble("position_lat", 30d); 
editor.putDouble("position_lon", 40d); 
editor.commit(); 
+1

正確には何か問題があるようですか?環境設定を保存する、または読み込む? –

+1

好みに何も保存することはできません。あなたは 'LatLong'オブジェクトを文字列、intなどに分割する必要があります –

+0

今はダブルで保存することができますが、今は問題があります。 –

答えて

3
sharedPreferences.getString("position", new LatLng(30,40).toString()); 

アプリケーションに「位置」に保存された情報がない場合は、代わりにデフォルト値を読み込む必要があります。ここはLatLngです(30,40)。 しかし、LatLngなどの複雑なオブジェクトは保存できません。

//load shared preferecnes 
SharedPreferences sharedpreferences = getSharedPreferences(this,Context.MODE_PRIVATE); 
//note: here you load the saved latitude and longitude values into the variable with name "loaded_position": 

LatLng loaded_position = new LatLng(0,0); 
loaded_position.latitude= sharedpreferences.getFloat("position_lat", 15f); 
loaded_position.longitude = sharedpreferences.getFloat("position_lon", 15f); 
Log.v("lastposition", "loaded position: ("+loaded_position.latitude+","+loaded_position.longitude+")"); 

// on edit preferences, save the LatLng Object: 
SharedPreferences.Editor editor = sharedpreferences.edit(); 
LatLng currentPosition = new LatLng(30f,40f); 
editor.putFloat("position_lat", (float) currentPosition.latitude); 
editor.putFloat("position_lon", (float) currentPosition.longitude); 
editor.commit(); 

このコードは、saved_positionにsaved(30,40)という名前のエディターを読み込みます。最初の起動時に15,15をロードする必要があります。これは、sharedPreferencesの最初の起動時に30,40が保存されないためです。

+0

読み込み用に - > sharedPreferences.getString(" position_lat ") ; ?またはどのように?ありがとう –

+0

はい、はい、私は、getDoubleを意味しますが、私は 'defaultvalue'フィールドに入れているのですか? thanks –

+0

default_valueはsharedpreferencesでキー "position_lat"に何も見つからない場合にロードされる値です。だから、答えのように任意の値を使用してください。 – MojioMS

関連する問題