简体   繁体   中英

How do I show options in a spinner that, when selected, save that option as an integer variable?

I'm working on an app that helps a rental business. I have a spinner that pulls in it's content from an array...

From datalist.xml :

<string-array name="dur"> 
    <item>@string/dur1</item>
    <item>@string/dur2</item>
    <item>@string/dur3</item>
    <item>@string/dur4</item>
    <item>@string/dur5</item>
    <item>@string/dur6</item>
    <item>@string/dur7</item>
    <item>@string/dur8</item>
    <item>@string/dur9</item>
    <item>@string/dur10</item>
    <item>@string/dur11</item>
</string-array> 

strings.xml :

<string name="dur1">Half hour</string>
<string name="dur2">1 hour</string>
<string name="dur3">2 hours</string>
<string name="dur4">3 hours</string>
<string name="dur5">4 hours</string>
<string name="dur6">5 hours</string>
<string name="dur7">6 hours</string>
<string name="dur8">Over night</string>
<string name="dur9">24 hours</string>
<string name="dur10">Week</string>
<string name="dur11">Month</string>

Spinner code in java :

Spinner spinner = (Spinner) findViewById(R.id.spinnerDuration);     
    ArrayAdapter <CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.dur, android.R.layout.simple_spinner_item);     
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter);        
    spinner.setOnItemSelectedListener(
        new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            int index = arg0.getSelectedItemPosition();
            // storing string resources into Array 
            dur = getResources().getStringArray(R.array.dur);
        }       
        public void onNothingSelected(AdapterView<?> arg0) { 
            // do nothing
        }
    }
);

How can I set it up so when the user selects an option from the spinner it then saves that option as an integer variable, reflecting the hours, which can then be used in other parts of this application?

For example...Someone selects "3 hours" and it saves a variable called 'durnumb' as 3. Or someone selects "Week" and it saves the value for durnumb as 168 (24hrs x 7days)

Save string as in shared preferences and retrieve it again anywhere in your app.

public class PreferencesData {

    public static void saveStringAsInt(Context context, String key, String value) {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        try {
            int intValue = Integer.parseInt(value);
            sharedPrefs.edit().putInt(key, intValue).commit();
        } catch (NumberFormatException e) {
            Log.e("PreferencesData", "Unable to parse to int: " + value)
        }
    }

    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return sharedPrefs.getInt(key, defaultValue);
    }
}

Usage:

// save
// do string manipulation before parsing e.g "weeks" = "168" or "3 hours" = "3" (for example substring away first encounter of whitespace)
PreferencesData.saveStringAsInt(context, "weeks", "25");
// retrieve
int weeks = PreferencesData.getInt(context, "weeks", -1);

As for the translation from weeks or "3 hours" to "3", I cannot give advice as it is very dependant on your needs. This should be a seperate method for each use case.

There is no reason why the translation should not convert to Integer directly and pass that to a method like PreferencesData.saveInt(), but your question seemed focused on String values, which is why it most likely would be convenient to convert from String to Integer 'on the fly' so to speak.

The most simple way is just to do such:

public static final int[] array = new int[]{1,2,4,6, etc...};

and get value by position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM