0

第一个项目在 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

        }

    });

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

4

1 回答 1

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"};
}



}
于 2012-05-29T11:10:59.370 回答