两个链接的微调器未保存为共享首选项

时间:2016-02-27 13:08:18

标签: android sharedpreferences spinner android-spinner

我可以使用Spinner not saving as Shared/Saved Preference将一个微调器保存为共享首选项。

然而,我需要多个微调器相互关联,当我尝试保存它们时我遇到了问题,我不知道为什么。

以下是我的微调器的链接方式。第一个微调器在选中时启用第二个微调器。

   spinner = (Spinner)findViewById(R.id.spnMondayExercise); 
    adapter = ArrayAdapter.createFromResource(this, R.array.Exercise, android.R.layout.simple_spinner_dropdown_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {



        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            long item = parent.getItemIdAtPosition(position);
            if (item == 0){
                monExc = 0;
                spinner = (Spinner)findViewById(R.id.spnMondayDuration); 
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner.setAdapter(adapter);
                spinner.setEnabled(false);
            }

这是另一个由上面的微调器启用的微调器

//Monday Duration
    spinner = (Spinner)findViewById(R.id.spnMondayDuration); 
    adapter = ArrayAdapter.createFromResource(this, R.array.Duration, android.R.layout.simple_spinner_dropdown_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {



        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            long item = parent.getItemIdAtPosition(position);
            if (item == 0){
                monDur = 0;
            }
            else if (item ==1)
            {
                monDur = .16;
            }
            else if (item ==2)
            {
                monDur = .33;
            }

这是我的loadData

private void loadData() {
SharedPreferences prefs = getPreferences(0);


    spnMondayDuration.setSelection(prefs.getInt("spnMonDur", 0));
    spnMondayExercise.setSelection(prefs.getInt("spnMonExc", 0));

这是我拯救偏好的地方:

SharedPreferences.Editor editor = getPreferences(0).edit();

                int spnMonDur1 = spnMondayDuration.getSelectedItemPosition();
                int spnMonExc1 = spnMondayExercise.getSelectedItemPosition();

                editor.putInt("spnMonDur", spnMonDur1);
                editor.putInt("spnMonExc", spnMonExc1);
                 editor.commit();

1 个答案:

答案 0 :(得分:0)

如果两个微调器的代码都在同一个类中,那么你完全弄乱了spinneradapter属性。为什么不为不同的微调器和两个不同的适配器创建两个变量?

第一个微调器的onItemSelected()方法是错误的。首先,您将变量指向第二个微调器,为它设置适配器,最后禁用它 - 我真的不知道你背后的意图是什么。您是否要重复使用相同的Spinner并使用不同的数据(因此具有不同的含义)填充它,或者您有两个单独的Spinner视图?

假设你有两个不同的Spinner,我会做这样的事情(代码仍然不够清楚,但用它作为起点):

// initialize Exercise spinner
spinnerExercise = (Spinner)findViewById(R.id.spnMondayExercise);
adapterExercise = ArrayAdapter.createFromResource(this, R.array.Exercise, android.R.layout.simple_spinner_dropdown_item);
      adapterExercise.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerExercise.setAdapter(adapterExercise);

// initialize Duration spinner
spinnerDuration = (Spinner)findViewById(R.id.spnMondayDuration); 
adapterDuration = ArrayAdapter.createFromResource(this, R.array.Duration, android.R.layout.simple_spinner_dropdown_item);
adapterDuration.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDuration.setAdapter(adapterDuration);

// disable by default until  an exercise is selected
spinnerDuration.setEnabled(false);


spinnerExercise.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, position, long id) {
        long item = parent.getItemIdAtPosition(position);
        if (item == 0){
            monExc = 0;
            spinnerDuration.setEnabled(true);
        }
        ........
    }
}

spinnerDuration.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
         long item = parent.getItemIdAtPosition(position);
        if (item == 0){
            monDur = 0;
        }
        else if (item ==1)
        {
            monDur = .16;
        }
        else if (item ==2)
        {
            monDur = .33;
        }
        ......
    }
}