如何让A Spinner访问其他Spinner?

时间:2012-02-05 19:54:15

标签: android-spinner

我有三个Spinners = spinState,spinCounty,& spinCity,我想选择State微调器然后选择一个State,然后第二个微调器会给我那个特定状态的县名单,然后选择County,然后第三个微调器会给我一个城市列表那个特定的县,如:(州)佛罗里达州,(县)戴德,(市)迈阿密然后经过3次选择后将这些信息传递给下一个活动/班级。有人可以帮忙吗?这是我的代码

Spinner spinState,spinCounty,spinCity;
Button bNext;

protected void onCreate(Bundle)
{
    //TODO Auto generated method stub
    super.oncreate(Bundle)
    setContentView(R.layout.info);

    Spinner States = (Spinner) findViewById(R.id.spinState);
    ArrayAdapter USstates = ArrayAdapter.createFromResource(this,
    R.array.States, android.R.layout.simple_spinner_item);
    USstates.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    States.setAdapter(USstates);
    Spinner Counties = (Spinner) findViewById(R.id.spinCounty);
    ArrayAdapter UScounties = ArrayAdapter.createFromResource(this,
    R.array.Counties, android.R.layout.simple_spinner_item);
    UScounties.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Counties.setAdapter(UScounties);
    Spinner Cities = (Spinner) findViewById(R.id.spinCity);
    ArrayAdapter UScities = ArrayAdapter.createFromResource(this,
    R.array.Cities,android.R.layout.simple_spinner_item);

    UScities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    Cities.setAdapter(UScities);

    initialize();
    bNext.setOnClickListener(this);
}

我应该使用什么代码?在哪里?PS。谁可以回答你可以使用我的确切变量,所以我不会感到困惑,在此先感谢。

1 个答案:

答案 0 :(得分:0)

countries.setOnItemSelectedListener(new OnItemSelectedListener() {
        ArrayAdapter<String> stateadapter=null;
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            switch (pos) {
            case 0:
                 stateadapter = new ArrayAdapter<String>(
                        activityclass.this,
                        android.R.layout.simple_spinner_item, Arrays
                                .asList(getResources().getStringArray(
                                        R.array.USAstate)));
                states.setAdapter(stateadapter);
            case 1:
                 stateadapter = new ArrayAdapter<String>(
                        activityclass.this,
                        android.R.layout.simple_spinner_item, Arrays
                                .asList(getResources().getStringArray(
                                        R.array.Indiastate)));
                states.setAdapter(stateadapter);
            }
        }



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

        }
    });

}

选择州

<string-array name="USAstate">
    <item>california</item>
    <item>texas</item>
    <item>virgina</item>
    <item>alaska</item>          
</string-array>