0

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

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) { }
    };
}
4

2 回答 2

0

弄清楚该怎么做:1)。提供两个私有变量(一个用于特定索引,一个用于通用索引)2)。从共享首选项 3) 中设置两个变量。在onItemSelectedListener中,设置具体的options 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) { }
    };
}

}

于 2013-07-31T00:54:39.200 回答
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作为默认首选项值然后使用-1with .setSelection(value);,它会导致您异常。如果返回 -1,则避免任何选择。

于 2013-07-30T02:50:09.287 回答