2014-11-05 69 views
1

我正在android中創建一個應用程序,我想存儲在谷歌地圖上選擇的地方用戶的數據。我目前通過將它們全部添加到一個數組中存儲所有的地方,然後通過Gson庫對它們進行序列化,並且它工作正常,並且編碼非常簡單和容易,但是如果我使用數據庫而不是那個那麼編碼將會更加複雜,因爲數據庫的植入更爲複雜,那麼只需將這些地方的數組串聯起來就可以共享偏好。下面是我的對象是存儲和保存在共享偏好的類,但如果想要將它們存儲在數據庫上,那麼我必須經歷更復雜的創建查詢插入,刪除更新等,所以建議我我應該使用db還是shred首選項可以很好地保存位置列表。將數據存儲在共享偏好和數據庫之間的差異android

package com.example.googlemapstext; 

import java.util.ArrayList; 

import android.location.Address; 

public class MyPlace { 
    private int id; 
    private String placeName; 
    private Address placeAddress; 
    private int ringerState; 
    private int brightnessState; 
    private int wifiState; 
    private int gpsState; 
    private int bluetoothState; 
    private int radiusValueIndex; 
    private ArrayList<Contact> contactArrayList; 
    private String message; 
    private double radiusValue; 
    private boolean notificationCheck; 

    public MyPlace(int id,String placeName, Address placeAddress, String radiusValue, 
      int ringerState, int brightnessState, int wifiState, int gpsState, 
      int bluetoothState, int radiusValueIndex, ArrayList<Contact> contactArrayList, 
      String message, boolean notificationCheck) { 
     this.id=id; 
     this.placeName = placeName; 
     this.placeAddress = placeAddress; 
     this.radiusValue = getTrimedRadiusValue(radiusValue); 
     this.ringerState = ringerState; 
     this.brightnessState = brightnessState; 
     this.wifiState = wifiState; 
     this.gpsState = gpsState; 
     this.bluetoothState = bluetoothState; 
     this.contactArrayList = contactArrayList; 
     this.message = message; 
     this.radiusValueIndex = radiusValueIndex; 
     this.notificationCheck = notificationCheck; 
    } 

    private double getTrimedRadiusValue(String radiusValue) 
    { 
     radiusValue=radiusValue.replace("Radius ", ""); 
     radiusValue=radiusValue.replace(" Meters", ""); 
     return Double.parseDouble(radiusValue); 
    } 

    public boolean getNotificationCheck() { 
     return notificationCheck; 
    } 

    public void setNotificationCheck(boolean notificationCheck) { 
     this.notificationCheck = notificationCheck; 
    } 

    public int getRadiusValueIndex() { 
     return radiusValueIndex; 
    } 

    public void setRadiusValueIndex(int radiusValueIndex) { 
     this.radiusValueIndex = radiusValueIndex; 
    } 

    public int getRingerState() { 
     return ringerState; 
    } 

    public void setRingerState(int ringerState) { 
     this.ringerState = ringerState; 
    } 

    public int getBrightnessState() { 
     return brightnessState; 
    } 

    public void setBrightnessState(int brightnessState) { 
     this.brightnessState = brightnessState; 
    } 

    public int getWifiState() { 
     return wifiState; 
    } 

    public void setWifiState(int wifiState) { 
     this.wifiState = wifiState; 
    } 

    public int getGpsState() { 
     return gpsState; 
    } 

    public void setGpsState(int gpsState) { 
     this.gpsState = gpsState; 
    } 

    public int getBluetoothState() { 
     return bluetoothState; 
    } 

    public void setBluetoothState(int bluetoothState) { 
     this.bluetoothState = bluetoothState; 
    } 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 

    public double getRadiusValue() { 
     return radiusValue; 
    } 

    public void setRadiusValue(String radiusValue) { 
     this.radiusValue = getTrimedRadiusValue(radiusValue); 
    } 

    public String getPlaceName() { 
     return placeName; 
    } 

    public void setPlaceName(String placeName) { 
     this.placeName = placeName; 
    } 

    public Address getPlaceAddress() { 
     return placeAddress; 
    } 

    public void setPlaceAddress(Address placeAddress) { 
     this.placeAddress = placeAddress; 
    } 

    public ArrayList<Contact> getContactArrayList() { 
     return contactArrayList; 
    } 

    public void setContactArrayList(ArrayList<Contact> contactArrayList) { 
     this.contactArrayList = contactArrayList; 
    } 

    public int getId() { 
     return id`enter code here`; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

} 
+1

可能是重複的:http://stackoverflow.com/questions/9590685/difference-between-shared-preference-and-sqlite – 2014-11-05 11:10:48

+1

創建一個json字符串並優先保存該字符串。 – GangaNaidu 2014-11-05 11:28:56

回答

2

SharedPreferences和數據庫之間的主要區別是像你提到的:

SharedPreferences的鍵值對的基礎上工作。您只需提供密鑰並獲取您存儲的價值。那很棒。

數據庫創建一個SQLite表,你需要使用查詢將其拉出。

我認爲,如果您使用的是您所建立的JSON機制,那麼在SharedPreferences中存儲字符串就是您所需要的。

但是,當數據變得越來越複雜,並且您希望快速訪問它的任何部分時,我認爲DB總是比解析和發送JSON字符串更容易。 是的,它可能會讓你寫處理數據庫查詢更多的代碼..

+1

在此處查看更多詳情:http://developer.android.com/guide/topics/data/data-storage.html – zaxy78 2014-11-05 11:17:50

2

我認爲SQLite會更適合你。我只用SharePreferences小,簡單和「鍵值」結構化數據。 (它應該是這樣的)

你有很多的數據,所以SQLite是要走的路。

閱讀以瞭解更多信息:Pros and Cons of SQLite and Shared Preferences

1

我認爲答案取決於你想要保存多少地方,你打算怎麼做,但我認爲數據庫是最好的方式去。

使用數據庫,您將能夠創建查詢以獲取您想要的位置,而不是將所有位置加載到列表中並在其中進行搜索。

爲了簡化數據庫創建(和使用),您可以嘗試Android的orm,如OrmLiteGreenDao。我認爲OrmLite比GreenDao更容易使用(但第二個似乎有更好的性能......),您可以找到多個示例there

在我看來,SharedPreferences應該只用於保存用戶偏好數據。