Search code examples
javaandroidpreferences

Retrieve number from android preferences


I'm probably missing something basic here, but I'm still a beginner. I have looked up a ton of websites but I can't seem to make any of those fixes work. I have a preference fragment that the user can input their hourly wage (among other things). I have an activity that is supposed to use that preference in a calculation, but I'm continually getting a 0 returned. Here's the code:

public class StartCalc extends Activity implements OnSharedPreferenceChangeListener {
static final String TAG = "StartCalc";

Date time_o, time_f;
float money_o = 0, money_f = 0, hoursToday = 0, hoursTotal = 0, payscale;
SharedPreferences prefs;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.registerOnSharedPreferenceChangeListener(this);

    payscale = prefs.getFloat("PAYSCALE", 0);
}

public float CalculateWeekly() {

        Log.d(TAG, "Calculating normaltime pay");
        float pay = 10 * payscale; // testing with 10 instead of hours
        // total
        Log.d(TAG, Double.toString(payscale)); //always getting 0.0
        return pay;

}

In my starting activity I have a toggle button that's supposed to calculate pay earned by comparing your hours worked to the payscale set in the preferences.

@Override
public void onClick(View v) {
    boolean on = ((ToggleButton) v).isChecked();
    StartCalc session = new StartCalc();
    Intent timeServiceIntent = new Intent(this, TimeService.class);

    if (on == true) {
        button = true;
        Log.d(TAG, "Button Clicked");
        // session.TimeClock();
        startService(timeServiceIntent);
    } else {
        button = false;
        // Log.d(TAG, "Not Running");
        // session.CalculateWeekly();
        try {
            tvTotal.setText(Float.toString(session.CalculateWeekly()));
        } catch (Exception e) {
            Log.d(TAG, "Set text didn't work...");
            e.printStackTrace();

        }
        Toast.makeText(Home.this, "Calculating...", Toast.LENGTH_SHORT).show();
    }

}

Solution

  • Well...

    You can't really instantiate activities like that, when you call new StartCalc() it doesnt call the onCreateMethod and payscale is 0.

    I dont even see a point to your StartCalc extending Activity since you're not setting a contentView, was it to get the context? You can get the context in other ways, try this instead:

    public class StartCalc implements OnSharedPreferenceChangeListener {
    static final String TAG = "StartCalc";
    
    Date time_o, time_f;
    float money_o = 0, money_f = 0, hoursToday = 0, hoursTotal = 0, payscale;
    SharedPreferences prefs;
    private Context context;
    
    public StartCalc(Context context) {
        this.context = context;
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.registerOnSharedPreferenceChangeListener(this);
    
        payscale = prefs.getFloat("PAYSCALE", 0);
    }
    
    public float calculateWeekly() { //its good practice to start method names with lower case
    
            Log.d(TAG, "Calculating normaltime pay");
            float pay = 10 * payscale; // testing with 10 instead of hours
            // total
            Log.d(TAG, Double.toString(payscale)); //always getting 0.0
            return pay;
    }
    

    Then on your onClick method try this:

    @Override
    public void onClick(View v) {
        boolean on = ((ToggleButton) v).isChecked();
        StartCalc session = new StartCalc(MainActivity.this); //im assuming its called main activity
        Intent timeServiceIntent = new Intent(this, TimeService.class);
    
        if (on == true) {
            button = true;
            Log.d(TAG, "Button Clicked");
            // session.TimeClock();
            startService(timeServiceIntent);
        } else {
            button = false;
            // Log.d(TAG, "Not Running");
            // session.calculateWeekly();
            try {
                tvTotal.setText(Float.toString(session.CalculateWeekly()));
            } catch (Exception e) {
                Log.d(TAG, "Set text didn't work...");
                e.printStackTrace();
    
            }
            Toast.makeText(Home.this, "Calculating...", Toast.LENGTH_SHORT).show();
        }
    
    }