动态设置Spinner不持久状态

时间:2013-07-30 01:59:34

标签: java android

我正在开发一个简单的应用程序,我遇到了动态创建Spinner的状态。无论我为动态微调器选择什么,它总是重置为定义的第一个元素。我已覆盖onPauseonResume但没有效果。任何帮助都会很棒。

public class MainActivity extends Activity {

private OnItemSelectedListener optionListener = getOptionListener();
// private OnItemSelectedListener specificListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ((Spinner)findViewById(R.id.optionsSpinner)).setOnItemSelectedListener(optionListener);
}

@Override
public void onPause()
{
    super.onPause();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int generalIndex = ((Spinner)findViewById(R.id.optionsSpinner)).getSelectedItemPosition();
    int specificIndex = ((Spinner)findViewById(R.id.specificOptions)).getSelectedItemPosition();

    prefs.edit().putInt("general", generalIndex).putInt("specific", specificIndex).commit();
}

@Override
public void onResume()
{
    super.onResume();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int generalIndex = prefs.getInt("general", -1);
    int specificIndex = prefs.getInt("specific", -1);

    ((Spinner)findViewById(R.id.optionsSpinner)).setSelection(generalIndex);
    ((Spinner)findViewById(R.id.specificOptions)).setSelection(specificIndex);
}

private OnItemSelectedListener getOptionListener()
{
    return new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {

            int resId;
            String selectedOption = parent.getSelectedItem().toString();
            if (selectedOption.equals(getString(R.string.color)))
                resId = R.array.colorsArray;
            else
                resId = R.array.shapesArray;

            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getBaseContext(), resId, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            ((Spinner)findViewById(R.id.specificOptions)).setAdapter(adapter);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) { }
    };
}

2 个答案:

答案 0 :(得分:0)

对于初学者来说,偏好Editor有问题。您需要提交此首选项:

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int generalIndex = ((Spinner)findViewById(R.id.optionsSpinner)).getSelectedItemPosition();
    int specificIndex = ((Spinner)findViewById(R.id.specificOptions)).getSelectedItemPosition();

   Editor editor = prefs.edit();
   editor.putInt("general", generalIndex);
   editor.putInt("specific", specificIndex);
   editor.commit();

否则您的偏好设置将不会被保存。

我想指出的另一件事是你需要避免将-1作为默认偏好值,然后将-1.setSelection(value);一起使用,这会导致异常。如果返回-1,则避免任何选择。

答案 1 :(得分:0)

弄清楚该怎么做: 1)。提供两个私有变量(一个用于特定索引,一个用于一般索引) 2)。从共享首选项中设置两个变量 3)。在onItemSelectedListener中,设置特定选项spinner

public class MainActivity extends Activity {

private int specificIndex;
private int generalIndex;
private OnItemSelectedListener optionListener = getOptionListener();
// private OnItemSelectedListener specificListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ((Spinner)findViewById(R.id.optionsSpinner)).setOnItemSelectedListener(optionListener);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    // Initial setup
    generalIndex = prefs.getInt("general", 0);
    specificIndex = prefs.getInt("specific", 0);
}

@Override
public void onPause()
{
    super.onPause();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    generalIndex = ((Spinner)findViewById(R.id.optionsSpinner)).getSelectedItemPosition();
    specificIndex = ((Spinner)findViewById(R.id.specificOptions)).getSelectedItemPosition();

    prefs.edit().putInt("general", generalIndex).putInt("specific", specificIndex).commit();
}

@Override
public void onResume()
{
    super.onResume();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    generalIndex = prefs.getInt("general", 0);
    specificIndex = prefs.getInt("specific", 0);
}

private OnItemSelectedListener getOptionListener()
{
    return new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {

            int resId;
            String selectedOption = parent.getSelectedItem().toString();
            if (selectedOption.equals(getString(R.string.color)))
                resId = R.array.colorsArray;
            else
                resId = R.array.shapesArray;

            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getBaseContext(), resId, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            ((Spinner)findViewById(R.id.specificOptions)).setAdapter(adapter);

            if (specificIndex != 0)
                ((Spinner)findViewById(R.id.specificOptions)).setSelection(specificIndex);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) { }
    };
}

}