在共享首选项和android中的数据库中存储数据之间的区别

时间:2014-11-05 11:05:47

标签: android

我正在Android中创建一个应用程序,我想存储用户在谷歌地图上选择的地点的数据。我目前通过将它们全部添加到数组中来存储所有位置,然后通过Gson库将它们序列化并且它工作正常并且编码非常简单且容易但是如果我使用数据库而不是那样那么编码将更复杂并且因为数据库的植入更复杂,所以只需将地点数组串联到共享首选项。下面是我的对象存储和保存在共享首选项中的类,但如果想将它们存储在数据库中,那么我必须经历更复杂的我必须创建插入,删除更新等的查询所以建议我我应该使用db还是shred首选项有助于保存地点列表。

 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;
    }

}

3 个答案:

答案 0 :(得分:2)

我认为SQLite会更好。我只对{strong>小型,简单和“键值”结构化数据使用SharePreferences。 (它应该是那样的)

你有很多数据,所以SQLite是最佳选择。

阅读本文以获取更多信息:Pros and Cons of SQLite and Shared Preferences

答案 1 :(得分:2)

SharedPreferences和DataBase之间的主要区别就像你提到的那样:

SharedPreferences 适用于键值对。您只需提供密钥并获取您存储的值。那很好。

DataBase 会创建一个SQLite表,您需要使用查询将它们拉出来。

我认为如果你对你构建的JSON机制很熟悉,那么只需在SharedPreferences中存储一个字符串即可。

但是当数据变得越来越复杂,并且您希望快速访问它的任何部分时,我认为DB比解析和搜索JSON字符串更容易。 是的,它可能会让您编写更多代码来处理数据库查询..

答案 2 :(得分:1)

我认为答案取决于你想保存多少个地方以及你打算用它们做什么,但我认为DB是最好的方式。

使用数据库,您将能够创建查询以仅获取所需的位置,而不会加载列表中的所有位置并在其中搜索。

为了简化数据库创建(和使用),您可以尝试使用OrmLiteGreenDao等Android版。我认为OrmLite比GreenDao更容易使用(但第二个似乎有更好的性能......)你可以找到多个例子there

在我看来,SharedPreferences只应用于保存用户偏好数据。