【问题标题】:Difference between storing data in shared preferences and database in android将数据存储在共享首选项和android中的数据库之间的区别
【发布时间】:2015-01-01 13:16:58
【问题描述】:

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

}

【问题讨论】:

标签: android


【解决方案1】:

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

SharedPreferences 基于键值对工作。您只需提供密钥并取回您存储的值。太好了。

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

我认为,如果您对自己构建的 JSON 机制感到满意,那么您只需要在 SharedPreferences 中存储一个字符串。

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

【讨论】:

【解决方案2】:

我认为SQLite 会更适合你。我只将SharePreferences 用于小型、简单 和“键值”结构化数据。 (应该是这样的)

你有很多数据,所以SQLite 是要走的路。

阅读本文了解更多信息:Pros and Cons of SQLite and Shared Preferences

【讨论】:

    【解决方案3】:

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

    使用数据库,您将能够创建查询以仅获取您想要的地点,而不是加载列表中的所有地点并在其中搜索。

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

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

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 2013-06-11
      • 2011-06-26
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 2023-01-25
      • 2011-08-23
      相关资源
      最近更新 更多