1

I have some spinners where I set values, four of them. I want them to retain the current settings between launches. When you select a value in the spinner it should not save that value until the updateSettings button is pressed. How do I go about this?

here is some code, in onCreate I make spinners with default values :

    mBaudSpinner = (Spinner) findViewById(R.id.bannerBaudSpinner);
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
            this, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mBaudSpinner.setAdapter(adapter);
    String[] tempArray = SlickUSB2Serial.BAUD_RATES;
    for (int i = 0; i < tempArray.length; i++) {
        adapter.add(tempArray[i]);
    }
     mBaudSpinner.setSelection(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal());


    mDataSpinner = (Spinner) findViewById(R.id.bannerDataSpinner);
    adapter = new ArrayAdapter<CharSequence>(this,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mDataSpinner.setAdapter(adapter);
    tempArray = SlickUSB2Serial.DATA_BITS;
    for (int i = 0; i < tempArray.length; i++) {
        adapter.add(tempArray[i]);

    }
    mDataSpinner
            .setSelection(SlickUSB2Serial.DataBits.DATA_8_BIT.ordinal());

    mParitySpinner = (Spinner) findViewById(R.id.bannerParitySpinner);
    adapter = new ArrayAdapter<CharSequence>(this,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mParitySpinner.setAdapter(adapter);
    tempArray = SlickUSB2Serial.PARITY_OPTIONS;
    for (int i = 0; i < tempArray.length; i++) {
        adapter.add(tempArray[i]);

    }
    mParitySpinner.setSelection(SlickUSB2Serial.ParityOption.PARITY_NONE
            .ordinal());

    mStopSpinner = (Spinner) findViewById(R.id.bannerStopSpinner);
    adapter = new ArrayAdapter<CharSequence>(this,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mStopSpinner.setAdapter(adapter);
    tempArray = SlickUSB2Serial.STOP_BITS;
    for (int i = 0; i < tempArray.length; i++) {
        adapter.add(tempArray[i]);

    }
    mStopSpinner
            .setSelection(SlickUSB2Serial.StopBits.STOP_1_BIT.ordinal());

If I click updateSettings, this code is run:

else if (v == mUpdateSettings) {
            if (mSelectedAdapter == null) {
                return;
            }

            mSelectedAdapter.setCommSettings(BaudRate.values()[mBaudSpinner
                    .getSelectedItemPosition()], DataBits.values()[mDataSpinner
                    .getSelectedItemPosition()],
                    ParityOption.values()[mParitySpinner
                            .getSelectedItemPosition()],
                    StopBits.values()[mStopSpinner.getSelectedItemPosition()]);

   } 

I am trying to use preferences like this for one spinner as an example, is it on the right track?

 SharedPreferences prefs;
         prefs = PreferenceManager.getDefaultSharedPreferences(this);
         SharedPreferences.Editor prefEditor = prefs.edit();
         prefEditor.putString("savedValue",mBaudSpinner.getSelectedItem().toString());
         prefEditor.commit();
         String username = prefs.getString("savedValue", String.valueOf(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal()));
         mBaudSpinner.setSelection(Integer.parseInt(username));

What do you do, do I make the sharedPrefs stuff global so that it can be used in both onCreate and in the button clicking listener? Then change the lines that set the default values for the spinners to something like this as an example:

String username = prefs.getString("savedValue", String.valueOf(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal()));
             mBaudSpinner.setSelection(Integer.parseInt(username));

Then put the other stuff about saving the values in the button listener? I'm trying this but the state doesnt save and always defaults to the wrong value. Is what I am doing right?

EDIT

These lines do not seem to set the default value right, why is this?

prefs = PreferenceManager.getDefaultSharedPreferences(this);
         String username = prefs.getString("savedValue", Integer.toString(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal()));
         mBaudSpinner.setSelection(Integer.parseInt(username)); 
Paul
  • 5,756
  • 6
  • 48
  • 78
  • If your class is a Activity, try to use `getPreferences` or `getSharedPreferences` instead of `PreferenceManager.getDefaultSharedPreferences(this);` – Bruno Mateus Mar 15 '13 at 12:15
  • I fixed that error, your way gave a nullpointer too. Apparently you cant use context globally? it causes a nullpointer, so I just set it up further down the code. Now my issue remains is that the dropdown is never set right – Paul Mar 15 '13 at 12:15
  • thanks bruno, i will look up the difference. It is an activity – Paul Mar 15 '13 at 12:16
  • @BrunoMateus .. why? I usually use what you suggested.. but what is the performance difference – stinepike Mar 15 '13 at 12:19
  • are you sure that you got the correct values with this `mBaudSpinner.getSelectedItem().toString()` ? – Bruno Mateus Mar 15 '13 at 12:19
  • If i change them I get: The method getSharedPreferences() in the type PreferenceManager is not applicable for the arguments (GraphicsTerminalActivity) – Paul Mar 15 '13 at 12:20
  • I should be getting teh right value, I will log it and see – Paul Mar 15 '13 at 12:20
  • also the spinner is never even right for the default value, even if I pass in "9600" hard coded. – Paul Mar 15 '13 at 12:21
  • It's weird, the spinner will show the wrong value when i start by default. But if I connect over serial the right value will be used and the value in the spinner changes to the right value. But the value always goes back to the wrong one on relaunch. – Paul Mar 15 '13 at 12:26
  • @Paul i really does not know. When i tried to use for the first time, i looked at the documentation and found that information; http://developer.android.com/guide/topics/data/data-storage.html#pref – Bruno Mateus Mar 15 '13 at 12:28
  • thanks. I will read that. I think the issue is with these lines, they do not set the preference right. Something about strings and ints maybe? prefs = PreferenceManager.getDefaultSharedPreferences(this); String username = prefs.getString("savedValue", Integer.toString(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal())); mBaudSpinner.setSelection(Integer.parseInt(username)); – Paul Mar 15 '13 at 12:30
  • So you are saving the correct values? If, yes, the values got wrong after relaunch because you are not loading them. You should read your sharedPrefences on the `onCreate` or `onResume` and set the correct values. – Bruno Mateus Mar 15 '13 at 12:35
  • I don't know why but changing to getPreferences made the default value right, thanks. – Paul Mar 15 '13 at 12:37
  • I thought this should load them: String username = prefs.getString("savedValue", Integer.toString(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal())); that is in oncreate – Paul Mar 15 '13 at 12:39
  • Look the explanation about the deference between, `getDefaultSharedPreferes` and `getPreferences` http://stackoverflow.com/questions/5652682/android-preferences-what-is-the-difference – Bruno Mateus Mar 15 '13 at 12:42
  • Look, when you call `mBaudSpinner.getSelectedItem()`you got a object and convert it to string. When you call `mBaudSpinner.setSelection(Integer.parseInt(username))` you get the string and convert to int, however, the `setSelection`should receive an int position and getSelectedItem return a object and not the item position. – Bruno Mateus Mar 15 '13 at 12:53
  • Ah i see, i am trying to set the value, rather than set the index! – Paul Mar 15 '13 at 12:58
  • Now to find a way to fix this without loads of if statements, you are very helpful thanks – Paul Mar 15 '13 at 13:00

1 Answers1

1

Look, when you call:

 mBaudSpinner.getSelectedItem()

you got a object and convert it to string. When you call :

 mBaudSpinner.setSelection(Integer.parseInt(username))

you get the string and convert to int. However, the setSelectionshould receive an int position and getSelectedItem return a object and not the item position.

Bruno Mateus
  • 1,727
  • 18
  • 25
  • thanks just trying to get the best way to receive the position, I seem to be doing it in a round about way – Paul Mar 15 '13 at 15:13
  • I don't think so. However you should refactor your code and decrease its size. You can use this adapter constructor to decrease the number of lines. `new ArrayAdapter(this, ,android.R.layout.simple_spinner_item, SlickUSB2Serial.BAUD_RATES);` You can change: `BaudRate.values()[mBaudSpinner.getSelectedItemPosition()]` to `mBaudSpinner.getSelectedItem()`. And the same for the others spinners. – Bruno Mateus Mar 15 '13 at 17:51
  • Thanks, I was able to get the index from the value so it's all good now. – Paul Mar 15 '13 at 19:08