0

I'm trying to locate shops in a map, the coordinates of this shops, I have to get it from the server, so I have two solutions, the first is to create SQLite database and update it when I get the informations, the second is to use SharedPreferences and store the data on it.

So I start with the second solution "SharedPreferences " because it will be fast and easy "when a user connect to the internet will get new data ,create new SharedPreferences and replace the old".

the question is I didn't find how to create in SharedPreferences contains multiple data like that :

shop 1
   +name
   + coordinate X
   +coordinates Y
shop 2
   +name
   + coordinate X
   +coordinates Y
shop 3
   +name
   + coordinate X
   +coordinates Y
...

I just found how to add for example your name or time (key/value) and update it

thank you

Abdel
  • 1,005
  • 2
  • 11
  • 24
  • 1
    SharedPreferences is just a key-value solution. If you want to stay with this solution, what you can eventually do is to concatenate your 3 informations separated by a character like ';'. A second solution would be to have 3 keys by store, like those: shopName1, shopX1 and shopY1. Hope this help. – Damien R. Sep 26 '13 at 10:29
  • thank you @Damien R , so witch solution you think is the best , and thanks for your help – Abdel Sep 26 '13 at 10:36
  • 1
    I would go for the second one, in case of the name of a shop would contain you separator character. – Damien R. Sep 26 '13 at 10:38

1 Answers1

1

You can do this inside a loop:

...
String data = name + ", " + coordinateX + ", " + cordinateY;
SharedPreferences prefs = this.getSharedPreferences("your.shared.preferences", Context.MODE_PRIVATE);
String savedShop = "your.shared.preferences.shop" + i;
prefs.edit().putString(savedShop , data).commit();

Then if you want to find your data, load your SharedPreferences and use StringTokenizer in order to parse the returned String.

By the way, I would rather use SQLite: it's more simple to do CRUD operation. Using SharedPreferences it's a suicide.

EDIT (after first comment on my answer)

I've asked to myself the same question when I started to develop Android app :-D !

Check Flo's answer: it's very clear and well explained (also read the first comment).

Community
  • 1
  • 1
JJ86
  • 5,055
  • 2
  • 35
  • 64
  • thnk you @JaAd, but why it's suicide, what's the problems I'll have later? – Abdel Sep 26 '13 at 11:58
  • thanks @JaAd, as I explain I have information about shops, that means Large amounts of data for device, SQLite need more work than SharedPreferences in the first time but when you need CRUD operation, SharedPreferences becomes convoluted, so I'll use SQLite. thank you – Abdel Sep 26 '13 at 12:57
  • Also, I believe instead of using .commit() you should use .apply() because it performs asynchronously. – LargeGlasses Dec 11 '13 at 17:45