将数据存储在共享偏好和数据库之间的差异android
我正在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;
}
}
SharedPreferences和数据库之间的主要区别是像你提到的:
SharedPreferences的键值对的基础上工作。您只需提供密钥并获取您存储的价值。那很棒。
数据库创建一个SQLite表,你需要使用查询将其拉出。
我认为,如果您使用的是您所建立的JSON机制,那么在SharedPreferences中存储字符串就是您所需要的。
但是,当数据变得越来越复杂,并且您希望快速访问它的任何部分时,我认为DB总是比解析和发送JSON字符串更容易。 是的,它可能会让你写处理数据库查询更多的代码..
在此处查看更多详情:http://developer.android.com/guide/topics/data/data-storage.html – zaxy78 2014-11-05 11:17:50
我认为SQLite
会更适合你。我只用SharePreferences
为小,简单和“键值”结构化数据。 (它应该是这样的)
你有很多的数据,所以SQLite
是要走的路。
可能是重复的:http://stackoverflow.com/questions/9590685/difference-between-shared-preference-and-sqlite – 2014-11-05 11:10:48
创建一个json字符串并优先保存该字符串。 – GangaNaidu 2014-11-05 11:28:56