1

Basically I am making an app that calculates compound interest for different amounts of money. There are 2 'Shared Preferences' if you will.

setup - for the user to enter interest rate, compounding frequency, term

preferences - for app setting (eg. show cents or not, set background color)

The program I am having is when I had one set of preferences it worked fine. However when I had two (when I created 'preferences'), it messed up the first set up preferences, called 'setup'.

Also, whenever it retrieves the preferences it is in a fragment.

Here is how I was getting preferences (When I didn't have the 2 preference xml's):

SharedPreferences getSetup = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
String t2 = getSetup.getString("rate", null);

Here is the solution I tried (opening the specific file and then of course using getSetup or getPrefs.getString("key, null")- it didn't work and always returned the default value)

SharedPreferences getPrefs = getActivity().getSharedPreferences("preferences",0); and for setup

SharedPreferences getSetup = getActivity().getSharedPreferences("setup",0);

Whenever I run the app, I get my error message ("please properly setup the app and enter all vales") This error message was setup to run with the following code:

SharedPreferences getSetup = getActivity().getSharedPreferences("setup",0);    
int t1 = value1.getText().length();
int t1_2 = value2.getText().length();

String t2 = getSetup.getString("rate", null);
String t3 = getSetup.getString("retirement", null);

String t4 = getSetup.getString("compounds", null);
String t5 = result.getText().toString();

if (t1 == 0 ||t1_2 ==0 || t2 == null || t3 == null || t4 == null) {
 //show the error message

Can someone please inform me what I am doing wrong and how I could get the specific set of preferences.

Cheers

andrew
  • 43
  • 6

2 Answers2

0

I think what you want is

SharedPreferences getSetup = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

You can see more about contexts at What's the difference between the various methods to get a Context?

Community
  • 1
  • 1
Peri Hartman
  • 19,314
  • 18
  • 55
  • 101
  • I get the error 'The method getApplicationContext() is undefined for the type new View.OnClickListener(){}' or if I put it at the start, The method getApplicationContext() is undefined for the type FragmentNormal – andrew Nov 23 '13 at 21:46
  • Can you show more code around where you get the context? And show the class declaration. – Peri Hartman Nov 23 '13 at 21:51
  • public class FragmentNormal extends Fragment and I tried your solution but with getActivity().getApplicationContext(), the application quit when it tried to return a value. Around the context, I was always using (getActivity().getBaseContext()) before I had two separate preference files – andrew Nov 23 '13 at 21:58
  • Well, what error are you getting from the system (not the error you display). A stack trace would help, including an indicator in your code sample where the failure occurs. – Peri Hartman Nov 23 '13 at 22:29
  • I don't get any error from the system - in my code it only moves on IF all the values are there. The problem I have now is that even though I specify the default value (eg. null - String t2 = getSetup.getString("rate", null); if it is empty it still returns "", not null – andrew Nov 23 '13 at 23:17
0

there is difference between getDefaultSharedPreferences and getSharedPreferences try as per below.

this defaultsharedpref is for the app specific shared preferences.

SharedPreferences getSetup = PreferenceManager.getDefaultSharedPreferences(getActivity());
        getSetup.edit().putString("rate", "200").commit();
        String rate= getSetup.getString("rate", null);

SharedPreferences are stored in an xml file in the app data folder, i.e.

/data/data/YOUR_PACKAGE_NAME/shared_prefs/reference.xml /data/data/YOUR_PACKAGE_NAME/shared_prefs/setup.xml

or the default preferences at:

/data/data/YOUR_PACKAGE_NAME/shared_prefs/com.android.example_preferences.xml

kedark
  • 244
  • 1
  • 7
  • I get 'The method getSharedPreferences(String, int) is undefined for the type FragmentNormal'. Also setup is the name of the XML file, not a specific preference in it. – andrew Nov 23 '13 at 21:43
  • when the application create this will create the default preference file. you are saving the preferene is another file (not in default one o.e preferences or set) and retriving from the default shared pref file. that is the reason you are getting the default set value i.e null ... Please let me know if it clarify your question – kedark Nov 23 '13 at 22:57
  • also if possible check out this below link http://stackoverflow.com/questions/6146106/where-are-shared-preferences-stored – kedark Nov 24 '13 at 03:43
  • Thanks for your help, I have it sorted now. I just didn't really understand how preferences worked. – andrew Nov 24 '13 at 07:07