I want to make an Android application which shows questions on the go on the basis of user selection. But I won't use a server, and so the questions have to be bundled with the app. But adding the whole questions would not be a great design, so either SQLite database can be used, or xml metadata can be used. But SQlite bundling I heard makes the app large in size. Is that so? And could someone explain how to refer to a xml file with self-defined metadata, to create questions on the fly. What will be the best way to do this?
the SQLite DB is on the phone already, I've used it a few times with no big jump in .apk size.
If you are looking for an easy-to-use stored hashmap, try SharedPreferences! Though I wouldn't use it for a heavy solution, the implementation guarantees ACID and is very straightforward. you can make several different hashmaps and name them different things with SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.html
I declare:
private SharedPreferences anchorHash;
in onCreate:
anchorHash = getSharedPreferences(getString(R.string.anchor_hash), MODE_PRIVATE);
inonPause:
SharedPreferences.Editor editor = anchorHash.edit();
editor.clear();
for( String s: anchors.keySet()){
//it's a another hashMap I want to write here, but you can do this however you like
editor.putString(s, anchors.get(s));
}
editor.apply();
//or commit() if you need to know about the success ( it'll happen )
Related
I am designing a simple application that will count how many times a user has tapped on a imageView. My question is what would be the best way of saving and reading this file. Any suggestions? I am thinking something like using Parse.com's local database. I have tried it, but I could not get it working the way I wanted. I am still a beginner, so please not so fancy suggestions.
Try to save data in SharedPreference. SharedPreference works like database for application on device that will be stored until any one has unistall app from device.
To create sharedPrefernce-
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To store data -
prefs.edit().putInt("key", int_value).apply();
To retrieve data-
// use a default value
int l = prefs.getLong("key", default_value);
Simplest options is always thebest option, go with shared preferences
Here is simple tutorial from google http://developer.android.com/training/basics/data-storage/shared-preferences.html
It will store your data in application local file. Take a note of that there are different shared preferences in example getPreferences() will return file specific for activity you used this method. While getSharedPreferences() will return application global file.
My app have had many releases and some of the earlier version sharedPreferences are useless.
What would be the best way to have the app itself sorting out the one that are still used and remove the unused one ?
Basically it would be like parsing the preferences XML's to extract the current android::key and by getting all preferences present on the device remove the one not being anymore in an XML.
Not sure I am very clear, but I have lot's of shared preferences not used anymore and having to establish the list manually can be source of problem.
Any hint would be great,
Thanks a lot
you can go through the keys one by one removing any that you dont use anymore like this
Map<String,?> prefs = pref.getAll();
for(Map.Entry<String,?> prefToReset : prefs.entrySet()){
if(prefToReset.getKey().equals("someKey")){
pref.edit().remove(prefToReset.getKey()).commit();
}
}
though there really is no performance benefit from doing this really
So what Im trying to achive is: When the user purchases a new item (a texture) they can click a button to replace the old Item(texture) with the new one.
What got in my mind first was Objectmaps. So I created this:
ObjectMap<Integer, Texture> screenMap = new ObjectMap<Integer, Texture>();
Im only using two items at the moment just to get a hang of it:
screenMap.put(prefs.getInteger("stoneOne", 0), sdStone);
screenMap.put(prefs.getInteger("stoneTwo", 1), stone_3);
Here´s the methods I use to change the texture:
public void setStone1() {
stoneImage = new Image(screenMap.get(0));
}
public void setStone(int screenId) {
stoneImage = new Image(screenMap.get(screenId));
}
And now to the part I can figure out:
Preferences prefs = Gdx.app.getPreferences("preferences");
prefs.putString("textField", textField.getText());
prefs.putString("textArea", textArea.getText());
prefs.getInteger("stone", );
prefs.putInteger("stone", );
prefs.flush();
I have no Idea what Integer to put in there as you can see I even left it out right now. I tried the screenId integer but it´s not reachable since it is in a void? P.S never mind the weird names I got for things. I took some code from an old project.
Try to walk for another way.
Save Texture to sdcard.
Save path to file into preferences.
Preferences prefs = Gdx.app.getPreferences("preferences");
prefs.putString("texture_one_path", "/sdcard/tex1.jpeg");
prefs.putString("texture_two_path", "/sdcard/tex2.jpeg");
Sergey is right. Textures are heavy and big, and you could potentially have tons of them. That's meant for the SD card.
Preferences are lightweight, secure configuration files used on mobile devices. They're usually encrypted, too, so accessing them always has a cost. They're used usually for user settings, login information, etc. The Android extrapolation of Preferences is called SharedPreferences. Here's the doc: http://developer.android.com/reference/android/content/SharedPreferences.html
The iOS concept of "SharedPreferences" is called NSUserDefaults, and here's its page: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html
Bottom line: you shouldn't be saving much information to the preferences. Use it for strings, global values, user settings (volume level, user credentials, turn off gore, etc.) For large string data, use raws. For large media, use SD card.
Hope that helps.
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 working on implementing the preferences for our application. I know how to display preferences UI and how to read/write values using SharedPreferences. In our app, I need to handle two sets of preferences and I would like to ask about this issue, one comment in the Android documents in particular.
The documentation for Preference.getSharedPreferences() has the following comment under the Return values section:
Returns The SharedPreferences where this Preference reads its value(s), or null if it isn't attached to a Preference hierarchy.
I would like to ask how it is possible to attach a SharedPreferences to a particular Preference, be it EditTextPreference or others. In other words, how does the persistence code in a Preference know that it should store the user input in one particular SharedPreferences object and not the other?
To explain my question further with an example, suppose I have the following:
SharedPreferences prefs1 = getSharedPreferences(file1, mode);
SharedPreferences prefs2 = getSharedPreferences(file2, mode);
My question is what API I should use so that prefs1 is used by the Preference objects' persistence code and not prefs2.
The target is Nexus One, running 2.3.4.
Maybe the answer is obvious but I could not find it after reading the documentation and searching the web. Thank you in advance for your help.
In other words, how does the persistence code in a Preference know that it should store the user input in one particular SharedPreferences object and not the other?
Preference uses PreferenceManager's getSharedPreferences(), which eventually routes to getDefaultSharedPreferences().
You are welcome to create your own Preference subclasses that change this behavior, but since the preference screen system may not be designed to handle multiple SharedPreference objects, there's a chance that your preference changes might not get persisted.
IOW, I encourage you to reconsider:
In our app, I need to handle two sets of preferences