2

I am creating an application in android and I want to store data of places user selected on the google map. I am currently storing all the places by adding them all in an array and then serialize them by Gson library and it works fine and coding is very simple and easy but if i use data base instead of that that then the coding will be more complex and because implantation of data base is more complex then simply string the array of places to shared preferences. below is the class whose objects are i am storing and saving in the shared preferences but if want to store them on the data base then i have to go through more complex I have to create queries for insert, delete update etc. so suggest me that should i use db or shred preference is good for saving list of places.

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

}
Furqan Ali
  • 137
  • 2
  • 13

3 Answers3

2

I think SQLite will be better for you. I only use SharePreferences for small, simple and "key - value" structured data. (and it should be like that)

You have a lot of data, so SQLite is the way to go.

Read this for more information : Pros and Cons of SQLite and Shared Preferences

Community
  • 1
  • 1
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
2

The main difference between SharedPreferences and DataBase is like you mentioned :

SharedPreferences works on an Key-Value pair basis. you simply provide the Key and get back the Value you stored. that's great.

DataBase creates an SQLite Tables and you need to use queries to pull them out.

I think that if you are good with the JSON mechanism that you built, then storing a string in SharedPreferences is all you need.

But when the Data get more and more complex, and you would like quick access to any part of it, I think DB would be easier than parsing and seaching a JSON string all the time. Yes, it might make you write more code for handling the DB queries..

AMI CHARADAVA
  • 303
  • 3
  • 9
zaxy78
  • 1,406
  • 3
  • 19
  • 32
1

I think answer depends on how many places you want to save and what do you plan to do with them but I consider DB as hte best way to go.

With a DB you will be able to create queries to get only places you want and not load all places in a list and search in it.

To simplify DB creation (and use) you can try orm for Android like OrmLite and GreenDao. I think OrmLite is easier to use than GreenDao (but second one seems to have better performance...) and you can find multiple examples there.

In my opinion, SharedPreferences should only be used for saving user preferences data.

Gaëtan Maisse
  • 12,208
  • 9
  • 44
  • 47