两个依赖的旋转器

时间:2012-05-29 11:02:08

标签: android spinner

第一个项目是用xml(<string-array>)定义的,但是第二个项目应该根据第一个项目中选择的项目呈现不同的项目数组...

使用AsyncTask从Web服务获取秒的可能字符串数组(此部分正在运行)。在我的onPostExecute(Void result)我有这个:

private class GetInfoTask extends AsyncTask<Void, Void, Void> {

    private ProgressDialog dialog = new ProgressDialog(StateTabActivity.this);

    //...
    @Override
    protected void onPostExecute(Void result) {
        Log.d("StateTabActivity","onPostExecute");
        sectorsArray = getSectorsName(); // sectorsArray is an array of strings
        roomsArray = getRoomsName(); // roomsArray is an array of strings
        subcategorySpinnerAdapter = new ArrayAdapter<String>(StateTabActivity.this, R.layout.my_spinner,sectorsArray);
        subcategorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        subCategorySpinner.setAdapter(subcategorySpinnerAdapter);
        dialog.dismiss();
    }
}

在我的活动onCreate()上:

Spinner categorySpinner = (Spinner) findViewById(R.id.statetab_category_spinner);
    ArrayAdapter<String> categorySpinnerAdapter = new ArrayAdapter<String>(this, R.layout.my_spinner,getResources().getStringArray(R.array.array_category));
    categorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    categorySpinner.setAdapter(categorySpinnerAdapter);

    subCategorySpinner = (Spinner) findViewById(R.id.statetab_subcategory_spinner);

    categorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Log.d("StateTabActivity","in onitemselected");
            switch (arg2) {
            case 0:
                    //I want to set here the items of sectorsArray to be displayed on the second spinner (subCategorySpinner)
                break;
            case 1:
                    //I want to set here the items of roomsArray to be displayed on the second spinner (subCategorySpinner)
                break;

            default:
                break;
            }

        }

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

        }

    });

所以我的问题是:我应该怎样做才能将正确的数组绑定到第二个微调器上,根据第一个选择的内容?

1 个答案:

答案 0 :(得分:0)

这是我根据选定状态获取地区列表的代码。

    final Spinner state = (Spinner)_activity.findViewById(R.id.state);
    final Spinner district= (Spinner) _activity.findViewById(R.id.district);
   _activity.findViewById(R.id.name_of_city);
    state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

       @Override
       public void onItemSelected(AdapterView<?> adapter, View v, int i, long lng) {



            if(i == 0){
                districtAdapter =new ArrayAdapter<CharSequence>( _activity ,              android.R.layout.simple_spinner_item, **DistrictList**.AndraPradesh); 
  //DistricList is another class.its code given below
                                          districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            district.setAdapter(districtAdapter);
                            if(_viewState == SAVED_VIEW){
                                                                                          district.setSelection(getArraySpinner(DistrictList.AndraPradesh,_initialValues.getAsString("District")),true);
            }
        }

在DistricList类中,

public class DistrictList {
    public static final String[] AndraPradesh = new String[] {"Adilabad",
        "Anantapur",
        "Chittoor",
        "East Godavari",
        "Guntur",
        "Hyderabad",
        "Karimnagar",
        "Khammam"};
}



}