I am developing a small libgdx game. I want to create sqlite database for storing game scores.
But i am not able to do the same as that i used in the activity. How to implement the same
Thanks in advance
Using a Database just to store scores seems to be a bit overpowered. Furthermore it won't be cross-platform compatible anymore, which is one of the major advantages with LibGDX.
I'd suggest you to do this either via:
an XML file: LibGDX: XML
a JSON file: LibGDX: JSON
Preferences: Preferences
With Preferences you would just store something like "level"="score".
With XML or JSON you could do much more, e. g. have a little class like the following:
public class Highscore {
private String username;
private String level;
private int score;
}
And then you would just write an array of those to JSON/XML and read it in as an array the next time you start the app. For a limited amount of highscores, this should be implemented pretty quickly and should not be a performance issue. Just load the list once on startup, and only update it when you detect a new highscore.
Related
If I have a class containing of integers, strings and a Date, how should I go about creating, saving and displaying a new instance of this class in my ListViews?
I've used SharedPreferences before for saving local strings but is this possible with objects as well, or should I look into SQLlite?
Edit: Also, if SQLlite is needed, a little steer in the right direction would be appreciated.
You can still use preferences to save your objects but you need to serialize them into strings. And for later use you have to deserialize them from strings into your list of objects. This can easily be done with Json.net but I would recommend to use a database like SQLite. This would make it easy if new requirements like searching or object extension comes up.
A good entry into SQLite and Xamarin can be found here or here.
I have following ArrayList in Android:
private ArrayList<Integer> Array= new ArrayList<Integer>();
It will grow over time via add()
It will approximately contain up to 50 elements.
I want to store it persistent. I was thinking of xml, sharedpreferences and DB, but I am not sure what is the best way to go.
Saving to SharedPrefs is probably the quickest.
Here is a question that shows how to marshal an array of strings into json and then store in SharedPreferences (and read them back again).
Just change the String array to an Integer array and you're done.
How can write code to make sharedpreferences for array in android?
I would use whatever else you are using in your app - ie keep similar things in similar places.
One benefit of using sharedpreferences is that they can be backed up (if you implement that service)... However using a DB may give you more flexibility in the future.
That doesn't really answer the question though. From how you describe it, it does smell like preferences would be better though.
I am creating an android application in which I am parsing so much XML data (XML data comes from a server) which contains Strings and image url's also.I need to use this data in many part of application. So for saving all these data I have used ArrayList and HashMap.I have declared ArrayList's and HashMap's variables in a single class as public static variables so I can access this data through a single class whenever I need.
And for images I have created ArrayList and placed in same single class same as other data(public static).Once I download a image through image url's I save those image drawables to these ArrayList variables so whenever I need any image again so I use these variables to get it.
Now my doubt is whether this approach is right or not. Please suggest me the right way.
Thank you
You should actually take a look at: http://developer.android.com/guide/topics/data/data-storage.html
It all depends on how you use the data, does it need to be updated all the time when the user opens the app you could take a look at this answer: How to declare global variables in Android? where you declare a global application and have the variables there.
Otherwise I would actually advice to use an sqlite database. http://developer.android.com/guide/topics/data/data-storage.html#db One good example is here: http://www.vogella.de/articles/AndroidSQLite/article.html
I'm a flash developer with no previous Java experience, just starting to learn android development. I'm trying to make a simple kid's flash cards app, consisting of a load of images of animals, and a load of sounds that they make.
Currently I have the images in a gallery view, storing them in an array. I also have an array of the sounds. So each image and the corresponding sound are in the same position in the array, so it's easy to play the right sound for the right image.
Now I want to shuffle the cards so that they appear in a different order each time the app is started up. I managed to shuffle the arrays into a random order, but kept the images and sounds in the same positions in each array but I can feel this getting messy and I'm sure this isn't the best way to go about this problem.
If this were a flash movie, I'd use objects to link the images and sounds and stick the objects in an array. Can anyone help me with some code which would achieve the same thing for android? Bear in mind I'm a complete beginner with Java and have gotten this far with tutorials and basic concepts being the same as AS3.
I'd use objects to link the images and sounds and stick the objects in an array.
Me too. Just create a class to wrap animals and sounds:
class SomeNiceName{
private final Bitmap animal;
// I'm guessing the sound is in the resources
// folder, thus you only need an integer to reference it
private final int sound;
public Animal(Bitmap animal, int sound){
this.animal = animal;
this.sound = sound;
}
public Bitmap getAnimal(){
return animal;
}// missing getter for sound
}
In this case I'm using an immutable object which is convenient in this case. Then you can create an array of those animals, o better yet a list:
// array
SomeNiceName[] array = new SomeNiceName[blah];
array[0] = new SomeNiceName(someBitmap, theSound);
// or with lists:
List<SomeNiceName> list = new ArrayList<SomeNiceName>();
list.add(new SomeNiceName(someBitmap, theSound));
The only thing you would have to "disorder" in this case is one array.
As Christian said you can of course use class in your Android application.
But, since mobile devices haven't huge processing capabilities like desktops or laptops -yet-, I advice you to read the articles below before running your OOP habits ;)
Object Creation
Getters/Setters? Not here!
For items above and more...
i have a basic function which requires serializing in my android app. The user will add some values to an ArrayList and i want to serialize it to avoid using a database for this little option and of course TO LEARN how to serialize (i'm a begginer) because it seems useful. Anyways the user save something in the arraylist, the program shuts down, the program starts up again and the user is able to see the saved data. How can i implement this? Can you provide some code snippet or a useful link?
Thanks a lot!!
You can do this by custom bean class and implement Serializable to that
so now when you create ArrayList<E> of that class it is Serializable.
Example:
Class dataBean implements Serializable
{
public String name;
}
ArrayList<dataBean> dataBeanArrayList = new ArrayList();
So dataBeanArrayList is now Serializable and you can also pass this between Intent.
I suggest using flexjson to serialize the data to a file. Then you can read that back using that library. This has several advantages over serialization which is being able to load your stream back into potential differing versions of your objects. Using ObjectInputStream you have to be very careful, and quite frankly I've never seen it work all that well.
http://flexjson.sourceforge.net
Here is a blog post how to do that:
http://wrongnotes.blogspot.com/2010/09/flexjson-meet-android.html