Storing/Retrieving Data with Shared Preferences - java

I have an application with 7 separate views. In each of those views there is an option to answer a 'yes' or 'no.' We are trying to save these 'yes' or 'no' values using shared preferences. Then, we want to have a new view/layout and be able to call those values from shared preferences. How do I go about doing that? My group and I have tried several different ways but cannot seem to get it to work. I know I don't have any code posted but it's because my code is just in bits and pieces. Thanks.

In your case I would not use getSharedPreferences() because it creates separate prefs files. Instead, you should use the main preferences file (call getPreferences() from the main Activity). This will be easier for you to manage than using shared preferences files.
I would write some code to show you how to do this, but the Android training page on this is about as clear as can be. Can you look it over and see if it answers your question? If not, post back here for clarification and I'll do what I can to help.
Here's the link to the page: https://developer.android.com/training/basics/data-storage/shared-preferences.html

Simple Do following Steps :
1)call getDefaultSharedPreferences() to create shared preferences
2) for each view just put values
3)in final view just create preference using getDefaultSharedPreferences() and then retrieve all the values
Hope this will help you

Related

JFace - How to remove preference property?

I'm wokring with a little JFace preference store where I can add preferences straight forward enough, but have trouble when I want to remove one of the preferences I've put in.
I can't see anything in the APIs that allows for removal. What is the correct way for doing this?
Assuming you are using IPreferenceStore you call setToDefault("pref id") to reset a preference to its default value.
This is weird !!
I asked this same question 3 days ago in the eclipse JFace forum: https://www.eclipse.org/forums/index.php/t/1088245
I only got answers not related to my need.
The answer is that it is not possible. Also you can not set the value or the default of a preference to "null"
In my app (JMSToolBox), the need is to "cleanup" the file that is backing the PersistenceStore as the user may store a lot of "keys/preferences" that may become obsolete at some point in time. I wanted a way to "remove/delete" them from the file to keep the PreferenceStore file as compact as possible.
I ended up writing my own version of PreferenceStore that exposes the "remove" method from the internal "Properties" object used by PreferenceStore. This class is not designed to fullfill my need.
Code is here

android: Changing values of constants/variables depending on the application using the activity

I am working on an AlertDialog Fragment which is supposed to pop up under certain circumstances, for example usage days of the app. I defined a constant for the current App and simply check if requirements are met before the onCreate is called like this:
if(statistics.getDays()>REQUIREMENT_VARIABLE)
{
onCreateTest=true;
}
Now I was planning to use this Activity with other applications/build variants (and change the values of the check variables for each individual app) however I was not sure how to go about checking which application is using it. Is a config xml where I define the packages in strings a good way to approach this? Thanks in advance.
I think you should define these variable in your app build.gradle specially for the build variant.
This post may help you.
Hope this helps.
Sorry for my english.
Create new java class along with your activity. Access and use that java class from another application to initialize check variables depends upon your business logic. And use those variables in the mentioned condition.

Android: Aggregating key value pairs in shared preferences

Is it possible to aggregate key value pairs in the same shared preferences from different activities?
For example, if I have a shared preference named "SP" and it has a key "SP_A" that I have stored it through activity A. This key has some value. How can I aggregate another key "SP_B" with some value, through activity B in the same shared preference "SP", such that in activity C, I can retrieve key values corresponding to "SP_A" and "SP_B" under the same shared preference "SP"?
And if this question is being downvoted, I would encourage to please write in comment your reason to downvote it. If some question is a bad question liable to be downvoted, people should know the reason why it is bad so that they improve their question asking skills and the quality of questions on this forum gradually improves. Anonymous downvoting and running away will not help anyone.
Once you create a SharedPreference using something like:
mContext.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
You can access it and write to it from anywhere in your app in the same way you created it. In fact, its just an xml file within your app's local storage.

Checkbox preference using .java

I am currently working on an android project where I need to create a SettingsActivity where I will have a variable number of checkboxes depending on an int from an SQLite database. I figured that the best way to do this was to create a for loop inside the SettingsActivity .java code, which would create a new checkbox preference for each time it runs the loop(as of now I only have a theoretical idea of how it would work). My problem would be to create the checkbox preference in .java rather than the XML. I searched the internet and could only find one example that had about 300 lines of code in it, and I can't believe that it needs to be as complicated as it was in that example. Does anyone have a helpful link, an idea how to do this or even knows a better way to make a variable number of checkboxes?
Sorry that I don't have any relevant code to post, but here is the link to the example I found http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/preference/CheckBoxPreference.java
http://developer.android.com/reference/android/preference/CheckBoxPreference.html
It also inherits a lot of methods from Preference: http://developer.android.com/reference/android/preference/Preference.html.
You can pass the constructor an attribute set where you specify summaryOn and summaryOff and the title
The link you posted is the source code of android. You don't need to implement that in your application as it's already in the android environment

First steps in Android architecture - please help me avoid a big mistake

I'm a relatively experienced .NET/iOS developer taking first steps in Android development - to help me avoid going down the wrong path I'd be grateful for some guidance:
Essentially, I have an app that displays locations on a map/list. As the user scrolls around the map, the locations are fetched from a JSON web service. A location can be tapped, at which point another JSON web service is called to retrieve live information for that location. The live info is then displayed.
So, having read the various 'getting started' Android docs, I would imagine I need:
An Activity to display the main map view of the locations
A second Activity to display the list view? These seems odd since I get the impression that each Activity has to be an entire screen of the app. I'd like to persist the other UI elements. (e.g. button to switch views, button for settings etc) Is this possible?
A Service (or IntentService?) to retrieve the locations from the web. How should it let the Activity and ContentProvider know when new locations have been retrieved - via Broadcasts or should they bind to it?
A ContentProvider, to cache and persist my locations. Perhaps the content provider should broadcast to the activities when new data is available to display?
Your help would be very much appreciated, since I feel a little lost!
Carlos
PS: I'll be developing with Mono for Android, unless enough people advise against
An Activity to display the main map view of the locations
Yes
A second Activity to display the list view? These seems odd since I
get the impression that each Activity has to be an entire screen of
the app. I'd like to persist the other UI elements. (e.g. button to
switch views, button for settings etc) Is this possible?
Not necessarily so. Take a look at the Fragments API. It allows you to switch only parts of your UI. It was introduced in Android 3.0, but there exists an official backport of it, so that you can also use it in previous Android versions.
With it, you can put your buttons into the activity, the map in one fragment and the list in another, and then just switch the map with list while retaining the buttons.
A Service (or IntentService?) to retrieve the locations from the web.
How should it let the Activity and ContentProvider know when new
locations have been retrieved - via Broadcasts or should they bind to
it?
I would strongly advice against this. You should use a service if you have long-lasting downloads in the background, like downloading a file or something. Short term JSON requests can and should be handled in the UI process. Use AsyncTask or an Executor for that. There has been advice by Google to put almost all of your requests into a service, but believe me, it's bull.
A ContentProvider, to cache and persist my locations. Perhaps the
content provider should broadcast to the activities when new data is
available to display?
Not required. You only really need a ContentProvider if you plan to make your content accessible to other Applications or the System. For HTTP caching, you can directly access the database/filesystem, or better yet, use the Apache HTTP Client Cache. Works well if you use the already embedded Apache HTTP Client, which you should.
Points 1 and 2 : You could use Fragments to update part of the screen, Activity will act as a container for multiple fragments ( use compatibility library for back porting fragments to API level 10 or less
You should use AsyncTask instead of a service to get the locations from a remote web service
AsyncTask has a callback onPostExecute(..) which will be called on completion of remote fetch, this can be used to update List, Maps or Fragments
1 . You can use MapActivity for map view;
2 . use Activity and place listView to include button in a single view instead of ListActiviy
3 .please follow the link for location updator tutorial
http://www.vogella.de/articles/AndroidLocationAPI/article.html
4 . use map overlay technique for your message display
please make comments if u want any suggestions further after u go through it

Categories