2016-09-13 55 views
-2

我有一個非常基本的應用程序,它使用Maps API來顯示地圖,長按之後它會在該位置放置一個標記。如果您選擇其他位置,它將刪除當前標記並創建一個新標記。在android中存儲少量數據的最佳方式

該應用程序做了我目前所需的操作,但是我可以在應用程序關閉後繼續使用數據。

這就是我正在嘗試做的:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     BufferedReader reader; 

     try { 
      final InputStream file = getAssets().open("userLatLng"); 
      reader = new BufferedReader(new InputStreamReader(file)); 
      String line = reader.readLine(); 
      line = reader.readLine(); 
      System.out.print("Getting Data"); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 

即用於應用程序啓動後

  String filename = "userLatLng"; 
      FileOutputStream outputStream; 

      try { 
       outputStream = openFileOutput(filename, Context.MODE_PRIVATE); 

       outputStream.write(Integer.parseInt(yayaParking.toString())); 
       outputStream.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

,應該是如何保存數據讀取數據。我還沒有得到這個工作,所以任何幫助,將不勝感激!

+3

將它保存在共享偏好設置中https://developer.android.com/training/basics/data-storage/shared-preferences.html –

回答

0

使用SharedPreference API爲小數據存儲。

SharedPreferences sharedPref = context.getSharedPreferences(
     "lat_pref", Context.MODE_PRIVATE); 

    SharedPreferences.Editor editor = sharedPref.edit(); 
    editor.putString(KEY_LAT, "12.7333"); // your lat and lng 
    editor.putString(KEY_LNG, "12.7333"); 
    editor.commit(); 

你可以像下面

SharedPreferences sharedPref = context.getSharedPreferences("lat_pref",Context.MODE_PRIVATE); 

String slat = sharedPref.getString(KEY_LAT, "0.0"); //0.0 is default value, you can change it to any value you like. This default value is applied when the key is not present in SharedPrefernce 

String slng = sharedPref.getString(KEY_LNG, "0.0"); 

//Convert String Lat and Lng to double values 
double lat = Double.parseDouble(slat); 
double lng = Double.parseDouble(slng); 

你可以在某個常數文件中聲明KEY_LAT & KEY_LNG檢索。

public static final String KEY_LAT = "latitude"; 
public static final String KEY_LNG = "longitude"; 

如果你在活動,然後使用this的背景下,如果在片段然後用getActivity()

編輯

editor.putLong(KEY_LAT, Double.doubleToLongBits(location.getLatitude())); 

檢索它像,

double lat = Double.longBitsToDouble(sharedPref.getLong(KEY_LAT, 0); 

待辦事項Longitu也一樣de

+0

顯然人們認爲這是一個愚蠢的問題,但它不喜歡'preference_file_key '也不'putDouble'(它們都被標記爲紅色)。 對不起,但我對Android非常陌生! –

+0

@EladKarni請檢查編輯答案。 – Aniruddha

+0

謝謝。我想我遇到了一些有關範圍的錯誤,因爲當我'setOnMapLongClickListener'時我無法引用正確的上下文。有任何想法嗎?我已經嘗試過'this'作爲上下文,但它不工作:( –

0

您可以將其保存在sharedPreferences上。

public String getLocation() { 
     SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
     return pref.getString("StoredLocation", ""); 
    } 

    public void setLocation(String value) { 
     SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
     Editor editor = pref.edit(); 
     editor.putString("StoredLocation", value); 
     editor.commit(); 
    } 

您可以使用上述代碼存儲和檢索位置。

0

製作一個SharedPreference的全局變量。

SharedPreferences preferences; 

保存數據爲:

private void savePreference(String your_data){ 

    preferences = getSharedPreferences("name_ur_preference", MODE_PRIVATE); 
    SharedPreferences.Editor editor = preferences.edit(); 

    editor.putString("DATA", your_data); 

    editor.apply(); 
} 
相關問題