I'm currently trying to think of the best way to program an app which simply has text and images that update every day to different content. Would the best way to do this be to store all of the items in an array and then call upon each according to the phone's clock? Or is there a better or simpler way of doing this?
If you need to know I'm using Eclipse to program the app.
You could have the app check for updates each day or you could make a mobile website and use a webview to display the site ,and just update the site daily
Since you are using text and images, I would recommend storing them in your app's SharedPreferences. These keep data stored that your app can easily access to view or change. You can store text and bitmaps easily. For text:
SharedPreferences prefs = getSharedPreferences("MySharedPreferences", Activity.MODE_PRIVATE);
SharedPreferences.Editor preferences = prefs.edit();
String firstString = prefs.getString("MyFirstString");
//check if firstString has changed on the server
//for example, set a new String that you retrieve from the server to firstStringMaybe
if (firstString != firstStringMaybe) { //meaning you need to update firstString
prefs.putString("MyFirstString", firstStringMaybe);
prefs.commit();
}
Bitmaps storage is much more complicated, because you need to first Serialize the bitmap, then store it as a String. This probably means creating a new class that contains the bitmap object and implements Serializable. There are many examples available for how to do this:
http://www.johnwordsworth.com/2011/06/serializing-and-de-serializing-android-graphics-bitmap/
android how to save a bitmap - buggy code
Related
I am using the Preference-API..
Typically when I need to retrieve the value of a Preference, I currently do something like this:
final SharedPreferences getPrefs =
PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean isThisPrefEnabled = getPrefs.getBoolean(REFERENCE_TO_PREF_NAME, false);
// OR
String theChosenPref = getPrefs.getString(PREF_NAME, DEFAULT_VALUE);
But I'm curious, couldn't I also do it like this? and if so, what is the difference?
Preference nameOfPref = findPreference(PREFERENCE_KEY);
boolean isPrefEnabled = nameOfPref.isEnabled();
// OR
String thePrefValue = nameOfPref.toString();
It seems to be more efficient, but the first example seems to be what get's used. Why is this?
Thanks.
SharedPreferences is an android specific interface documentation
android.content.SharedPreferences : is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.
Preferences is a core java class documentation
java.util.prefs.Preferences : This class allows applications to store
and retrieve user and system preference and configuration data. This
data is stored persistently in an implementation-dependent backing
store.
I'm making an Android application and want to create a "Favorites" list for some objects in the app. I wanna make the list accessible and editable in all my activities and I can't really figure out the best way to do this.
Shared preferences? Writing a small txt file to the device? What's the fastest way to do this?
Thanks in advance!
dependencies {
compile 'com.google.code.gson:gson:2.3.1'
}
Then when you want to save, convert your array into String:
ArrayList<Type> yourData = new ArrayList<Type>();
String dataStr = new Gson().toJson(yourData);
//put this dataStr in your shared preferences as String
To retrieve and convert back to an object is also simple:
String str = "";
//you need to retrieve this string from shared preferences.
Type type = new TypeToken<ArrayList<Type>>() { }.getType();
ArrayList<Type> restoreData = new Gson().fromJson(str, type);
If you want to create a Favorites list, use a SQLite Database.
There's really only four ways to store data.
Shared Preferences
Databases
Local files
Remote Server - Slowest since it depends on network connection, so let's skip this.
Between the remaining 3, SharedPreferences is a great option when used to store a single value. However, it's not a good option for storing a Favorites list, mainly because you can only store a single String.
You can still store it by combining all items in your list into one string, then splitting it each time. However, as your Favorites list gets larger, this single long String will too. Splitting and combining all the time isn't efficient.
SharedPreferences is still a decent option if you only have to store the Favorite's list, but since you want to edit it too, it becomes a less attractive solution.
Local Files and Databases are the better options, however local files require you to read in the file each time you want to use it. This process of reading and writing to a file isn't as efficient as using a Database, especially if you want to edit. For example, let's say you want to remove an item from the middle of your Favorite's list. This would require you to read in the file, edit it, then write the change into the file again. Not too pleasant when compared with the ease of the final solution.
Databases are the best option for this, mainly because it's designed to manage data. You can create a single Favorite's table and add each item as it's own individual row. Fetching the entire table becomes quick and easy. Fetching a single item becomes quick and easy. Adding a new item or removing a new item is also quick and easy.
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
While developing an android app, which is the best and efficient way to retrieve values from the below options:
To get values from a property file or
To get values from app shared preferences.
I like to know this based on memory usage and speed of retrieval.
Edit:
IMO there's no better way as those two options would be used for different purpose.
SharedPreference gives you an easy way to build a user editable Preference page via the PreferenceActivity and to share preference trough your application.
For other purpose as user editable settings you could use Properties files to store data.
For properties you have two good examples here:
http://www.mkyong.com/java/java-properties-file-examples/
http://myossdevblog.blogspot.com/2010/02/reading-properties-files-on-android.html
For SharedPreference see here:
http://developer.android.com/guide/topics/data/data-storage.html#pref
An example:
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final String yourPref = sharedPreferences.getString(getString(R.string.settings_tag_your_pref), "");
or if you don't want to define a tag in string:
final String yourPref = sharedPreferences.getString("your_pref", "");
If you have time , please can you take some time for me ? I really need some help.
Let me explain;
Im working on an android app.
There is a layout and it has 5 ImageButton and a webview. When the users click on a imagebutton, without problem it calls a website below..but more or less i have 20 web site.i want to add an option for users..for example the user will choose a web site from Prefs. screen then automatically one of imagebuttons values (i mean icon AND its loadurl function) will change.
I created Pref Screen and i can see my website in this which i wrote in array.xml
but totally im not able to set them to Imagebuttons..
im beggin u.its our last curve..then it ll finish.
Im tried to use this code :
Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
but i couldnt.
Please explain me step by step clearly.
also i dont want that only for me,on internet there is no source for this issue.im searching and trying everything what i can think more than 6 days but nothing.
Thank you so much
SharedPreference Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
Basically what this does is allows you to store user information such as scores for a game, stats, and other variables.
The first line gets reference or creates the file to write the data to.
The second line allows you to edit the file to write new information to it..
e.putString()
Takes two parameters the first one being the Key to pull the value you out later, and the second being the value you want to put into the file.
The last line commits the data so that it is saved to the file.
You can get more info from the docs here
Also if you want to pull the data out just do
SharedPreference Data = getSharedPreferences(filename, 0);
String value = Data.getString("website"); // use the key here to pull the data out
EDIT:
So for example if the user selects a certain image you could use a key to refer to each image and get the value later to decided which icon the user picked before.