I have a game where, at the end, if the player's score is higher than any of the saved highscores, they are asked to type in their name in order to save their score. I also have an Activity where the five highscores are displayed, next to the name provided for each one of them.
I don't know how to go about saving the highscores with the name the player provides. I've thought about an Arraylist containing instances of a Highscore class, which store a name and a score, but it seems like overkill.
How should I accomplish this?
Make use of this library to store data using shared preferences
https://github.com/Pixplicity/EasyPrefs
Related
I've made a little android game and I have a problem saving the overall score which should be retrieved every time the app starts up again instead of just starting at the default 0 like it always does.
I looked at SharedPreferences but that seems to be more about saving high scores that won't change.
Is there a way for my save the a score which will be changed as more games are played?
I assume I'd need to do the following:
Retrieve the saved score
Put it into a variable
Change that variable
Save it again end of game or every-time it changes (depending on whether or not it affects performance)
Edit: Turns out SharedPreferences would work for this scenario. Now could someone please provide an answer of how to implement it?
I believe you can use Shared Preference for that purpose.
It can be used to save your score, overwrite the score and retrieve the score.
Shared preference uses the Key value pair.
I'm not sure why you'd come to the conclusion that SharedPreferences would be more suitable for non-changing values.
I think this is an excellent use case for it.
You could try other persistence options (such as integrating SQLite into your app) but this seems like the quickest option with the least amout of overhead.
Still, if you're interested in other options, I hope you've skimmed through the storage options in the Android documentation:
http://developer.android.com/guide/topics/data/data-storage.html
And, of course, using SharedPreferences will have to be done in conjunction with Application LifeCycle methods (such as onResume, onPause, etc):
http://developer.android.com/training/basics/activity-lifecycle/index.html
You can use a SQLite database to save the score value.
To retrieve you current score in the database use this query.
SELECT score from ScoreTable where id = userId;
Assign the retrieve value to a variable.
At the end of the game // Update the score value
int finalScore = initialscore + newScore
UPDATE ScoreTable SET score = finalScore WHERE id = userId
Iam working on an android application and have trouble making a decision for the architecture saving application data.
Following case:
In the app the user has the possibility to create new general objects and give them properties he want. To support this, i want to give them a list with favorites before creating the input form, for example a car. It has color, weight, speed, horsepower etc.
So the user can choose a often picked object (for example the car) and will get the appropriate fields for the form he has to fill (color, weigth ...).
This list should be smart. The more you pick an item, the higher it appears in the list. And this presets have to be editable in preferences.
And thats the point. Should I implement my idea with the preferences framework from android (save it to xml as different preferences types and simply load due preferencebuilder) or should i create own xml objects and save it to self created user file location?
My second question: if i use the preference framework method .... is this made good for dynamically add entries at runtime? the ressources are in the res folder, but what if there are individual user entries? will they also be saved in the program folder or is there a special user data folder where the files (maybe encrypted) are in?
Thank you
Storing such complex data in SharedPreferences is tricky. What I mean is assuming user created 4 objects and each has 8 properties. You would store 4*8 values in sharedprefs and map them too.
What can be done is maintain an array list of objects created by user. Consecutive to that list maintain counter array list and keep swapping both lists internally as per number of times user has clicked the object. example:
List Name List Counter
ObjA 5
ObjB 3
ObjC 1
ObjD 1
Store these two lists in Shared Prefs.
Now, for the object's properties part (2 possibility arises) :
Maintain a mySQL DB and a table for each object's name. You can store values of each column in it IF you need to store every instance created of the object by user. (every time user clicks the object just show him/her the column names of table and store the values entered)
Example :
ObjA Table :
Color speed horsepower rpm
________________________________
red 20mph 100 3000
black 80mph 500 8000
Consecutively, if you don't want to store every instance value, you can make another sharedPrefs with object as key and an Arraylist of properties as value.
I have some scens with and I want save their state of view when app is closed. Some times it's one thing like "is that scene has opened shop window?" but sometimes ther is much more info which I need to store. So is better to use for it UserDefaults/SharedPreferences (json to string) or create model, serialize those info to model then save it to DB?
I will be thankful for Your opinion.
I use SharedPreferences when i have to save a variable, such as UserName, Country of Origin, access token, location, UsersCurrentLevel, UsersCurrentHealth etc.
I use Database when i have data that requires much manipulation or has more specifications to it. Such as questions for users,
video ad's details(seenState , directoryPath, urlToDownloadVideo, hasVideoBeenAlreadyDownloaded etc.), lists of data, etc.
So i would suggest you use a mix depending on the data.if it has a single entry use SharedPreference else if it has a list/ multiple data for a single format put it in a DB. Hope this helps.
I have two edittext and save button. When application is started, last data will coming. I know SQL ite database for it but can I use a text file or another thing. Is Sqlite correct always? And can I give example for save data from two edittext , and I get data to edittext.
Developers Guide says:
"If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs"
This is the problem:
I have a form for inputting names, then saves it, then displays it in another activity. But once I try to enter another name, the previous name is overwritten by the new name.
Is there a way to set these names up to list themselves one after another without overwriting each other in SharedPreferences?
You can as long as they have distinct names. Ig you need multiple values for same name, you can store JSON array or use some form of prefix / suffix solution to provide unique names
Either do it like Konstantin Pribluda suggested, or you might think of using the SQLite, if you have a lot of names you want to store (e.g. if creating a history of typed in names). That way you can store unlimited values for the same key and retrieve them as a list/cursor.
But of course that's overkill if you only have 2-3 names…
you can also save a Set of Strings in the SharedPreferences.