将值从1个android微调器传递给下一个微调器

时间:2013-08-21 19:13:45

标签: android spinner

嘿,我想问一下如何将选择的值从1个微调器传递给下一个微调器。例如:

Spinner 1 = "School" is selected
Spinner 2 = Shows sub items for the selected item "School"

OR

Spinner 1 = "Office" is selected
Spinner 2 = Shows sub items for the selected item "Office".

2 个答案:

答案 0 :(得分:0)

在第一个微调器(Spinner 1)的onItemSelected列表器中的第二个微调器(微调器2)中绑定数据

Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

          // bind data in second spinner as per you select 
          // in this first spinner (Spinner 1)
        }

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

        }
    });

答案 1 :(得分:0)

查看此示例代码,这肯定会帮助您

public class MainActivity extends Activity {

Spinner s1,s2;
Button btn;
String s;
TextView tv;
ArrayAdapter<String> adap1,adap2,adap3; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    s1=(Spinner)findViewById(R.id.spinner1);
    s2=(Spinner)findViewById(R.id.spinner2);
    btn=(Button)findViewById(R.id.button1);
    tv=(TextView)findViewById(R.id.tv1);


    String[] v1=getResources().getStringArray(R.array.c1);
    String[] v2=getResources().getStringArray(R.array.c2);
    String[] v3=getResources().getStringArray(R.array.c3);

    adap1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v1);
    adap2=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v2);
    adap3=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v3);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public class ItemSelectedListenerr implements OnItemSelectedListener  {

    String[] v=getResources().getStringArray(R.array.c1);
    public String s;
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

        s=arg0.getItemAtPosition(arg2).toString();
        if(arg2==0)
        {
            s2.setAdapter(adap1);
        }
        if(arg2==1)
        {
            s2.setAdapter(adap2);
        }
        if(arg2==2)
        {
            s2.setAdapter(adap3);
        }

        tv.setText(s);
        Log.i("hahaha", "item selected is"+s);

    }


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

    }

}

}

它将像魅力100%一样工作。 :d