Spinner内容如何依赖于另一个Spinner的选定项目?

时间:2017-11-21 19:49:20

标签: java android

我需要spinner2内容依赖于spinner1中的所选项目,此代码spinner2仅从case0 : R.array.subMenuTrans获取其内容,无论选择何种内容spinner1

package com.example.game1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class DialogMenu extends AppCompatActivity { 

    Spinner spinner1;
    Spinner spinner2;    **spinners**
    Spinner spinner3;
    ArrayAdapter<CharSequence> adapter1;
    ArrayAdapter<CharSequence> adapter2;
    ArrayAdapter<CharSequence> adapter3;

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

        spinner1 = (Spinner) findViewById(R.id.menu);
        adapter1 = ArrayAdapter.createFromResource(this, R.array.menu, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override // TODO Auto-generated method stub
            public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
                Toast.makeText(getBaseContext(), spinner1.getSelectedItemPosition() + " is selected.", Toast.LENGTH_LONG).show();
            }

            @Override// TODO Auto-generated method stub
            public void onNothingSelected(AdapterView<?> adapterView) {

            }

        });

        spinner2 = (Spinner) findViewById(R.id.submenu);
        int options = spinner1.getSelectedItemPosition();
        switch (options) {
            case 0://spinner2 takes only content of case0,should take all cases depenting on selection of spinner1....
                adapter2 = ArrayAdapter.createFromResource(this, R.array.subMenuTrans, android.R.layout.simple_spinner_item);
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                break;
            case 1:
                adapter2 = ArrayAdapter.createFromResource(this, R.array.subMenuSights, android.R.layout.simple_spinner_item);**takes content**
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                break;
            case 2:
                adapter2 = ArrayAdapter.createFromResource(this, R.array.subMenuEdu, android.R.layout.simple_spinner_item);
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                break;
            case 3:
                adapter2 = ArrayAdapter.createFromResource(this, R.array.subMenuHeal, android.R.layout.simple_spinner_item);
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                break;
            case 4:
                adapter2 = ArrayAdapter.createFromResource(this, R.array.subMenuBus, android.R.layout.simple_spinner_item);
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                break;
            case 5:
                adapter2 = ArrayAdapter.createFromResource(this, R.array.subMenuAthl, android.R.layout.simple_spinner_item);
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                break;
            case 6:
                adapter2 = ArrayAdapter.createFromResource(this, R.array.subMenuPubServ, android.R.layout.simple_spinner_item);
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        }
        spinner2.setAdapter(adapter2);

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override// TODO Auto-generated method stub
            public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
                Toast.makeText(getBaseContext(), spinner2.getSelectedItemPosition() + " is selected.", Toast.LENGTH_LONG).show();
            }

            @Override// TODO Auto-generated method stub
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

你将spinner2在onCreate()中的位置设置为spinner1的位置(在onCreate()期间)。

onCreate()只被调用一次,所以spinner2的位置在这种情况下只被设置一次。

一种解决方案是从spinner1的onItemSelected()方法中改变spinner2的位置。

答案 1 :(得分:0)

你的onItemSelected必须触发spinner2 init