I can't figure out how to use android PreferenceActivity

I'm trying to use preference activity in android, I set everything up, but I have problems getting user preferences. To be specific, these are two options for edittext, the length and the length of the year, which I want to get as int values, so I can use them when calculating in another activity.

Every example I find is different and I cannot find a clear explanation. I will publish the class and method that I use below, I know the code that is not correct, but I feel that I have come across it all the time, and I can’t figure it out. In this line, in particular, I do not believe that this is correct: SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); and I really don't understand the effect of preferences in general, they don't seem to work the way I expected. Any advice would really be great! I manipulated the same lines of code several times.
Thank you

The action of my preferences is as follows:

 package com.geistware.studentbudgetapp; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.text.method.DigitsKeyListener; import android.util.Log; public class UserPrefs extends PreferenceActivity { public int termLength = 1; public int yearLength = 1; @Override //OnCreate never runs, why? protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.setup); EditTextPreference usersTermLength =(EditTextPreference) findPreference(getString(R.string.term_length_set)); //finds //the edittext box by its key, term_length_set usersTermLength.getEditText().setKeyListener(DigitsKeyListener. getInstance()); //DigitsKeyListener only allows digits to be typed in Log.v("UserPrefs", "UsersTermLength: " + usersTermLength); EditTextPreference usersYearLength =(EditTextPreference) findPreference(getString(R.string.year_length_set)); usersYearLength.getEditText().setKeyListener(DigitsKeyListener. getInstance()); } } 

and I want to be able to get users termLength and yearLength (as ints) in this method:

 public float alterForFrequency(float enteredAmount, String spinnerPosition){ //get user preferences (not working) SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String termKey = getString(R.string.term_length_set, ""); String yearKey = getString(R.string.year_length_set, ""); int termLength = prefs.getInt(termKey, 1); int yearLength = prefs.getInt(yearKey, 1); Log.v("alterForFrequency", "userprefs: " + termLength + yearLength); //perform calculations for monthly/temly/yearly if(spinnerPosition.equals("Monthly")){ usersAmount = ((enteredAmount / 31)* 7); Log.v("FiscFreq", "revisingAmount=" + usersAmount); } else if(spinnerPosition.equals("Termly")){ usersAmount = (enteredAmount / termLength); Log.v("FiscFreq", "revisingAmount=" + usersAmount); } else if(spinnerPosition.equals("Yearly")){ usersAmount = (enteredAmount / yearLength); } else{ usersAmount = enteredAmount; } currencyRevisedAmount = toCurrency(usersAmount); //drops the unwanted digits return currencyRevisedAmount; } 

I actually create an instance of FiscalFrequency to use alterForFrequency in an activity called Keypad, for example:

 public class Keypad extends Activity { ..... public float reviseOnSpinnerPos(){ enteredAmount = Float.valueOf(userAmount.getText().toString()); FiscalFrequency ff = new FiscalFrequency(); revisedAmount = ff.alterForFrequency(enteredAmount, spinnerPosition); return revisedAmount; } } 
+4
source share
2 answers

GetDefaultSharedPreferences (Context) accepts an instance of Context (as it says), and what you supply is not context. If your FiscalFrequency class is in action and is not static, try calling

 getDefaultSharedPreferences(UserPrefs.this) 

Or no matter what you are currently doing (as Activity expands the context)

Otherwise, put a context instance when instantiating your FiscalFrequency class. One way is to do this through the constructor.

PS It would be easier if you sent a piece of code where you create an instance of FiscalFrequency and activity where it happens.

EDIT: In your FiscalFrequency class, do the following:

 public class FiscalFrequency { public FiscalFrequency(final Context context) { this.context = context; } private Context context; ... the rest of your class ... } 

And you can create an instance of your FiscalFrequency only when the context is supplied.

 FiscalFrequency ff = new FiscalFrequency(this); 

("this" is a reference to the class we are currently in, and this class in your case is Activity, and Activity is context.

And finally, in your alterForFrequency () do:

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 

"context" is a reference to the Context (your activity class in your case) that you specified when creating the FiscalFrequency instance.

+4
source

Try adding a listener at the bottom of the onCreate method in the UserPrefs class:

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); prefs.registerOnSharedPreferenceChangeListener(this); 

This will cause the settings to change on the fly when the user changes the values.

+1
source

Source: https://habr.com/ru/post/1337121/


All Articles