Question

I've been trying to use an android preference activity, I got it all set up but I'm having trouble actually retrieving the users preference. To be specific it's two edittext preferences, term length and year length, that i want to retrieve the values of as int's so i can use them in a calculation in another activity.

Every example i find is different and I can't really find a clear explanation. I'll post the class and the method I'm using below, I know the code that's there isn't correct but i feel like I've messed around with it every which way now and can't figure it out. This line in particular i don't believe is correct: SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); and i don't really understand preference activities as a whole, they don't seem to work as I'd expect. Any advice would really be great! I've been juggling the same lines of code about for days.
Thanks.

My preferences activity looks like this:

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 retrieve the 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'm actually creating an instance of FiscalFrequency to use alterForFrequency in an activity called Keypad like so:

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;
    }
}
Was it helpful?

Solution

The getDefaultSharedPreferences(Context) takes in Context instance (as it says), and what you are supplying is not a context. If your FiscalFrequency class is located in activity and is not static, try calling

getDefaultSharedPreferences(UserPrefs.this)

Or whatever activity you are in currently (since Activity extends Context)

Otherwise, supply Context instance when you create an instance of your FiscalFrequency class. One way is do it through constructor.

P.S. Would be easier if you would post a part of the 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 yhen you can create an instance of your FiscalFrequency only when supplying context to it.

FiscalFrequency ff = new FiscalFrequency(this);

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

And finally, in your alterForFrequency() do:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

"context" is a reference to Context (your activity class in your case), that you provided when you created an instance of FiscalFrequency.

OTHER TIPS

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

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

This will cause the preferences to be changed on the fly when the user changes the values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top