2012-05-29 81 views
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 

     } 

    }); 

所以我的問題是:我應該怎麼做才能正確的陣列到第二微調綁定,根據第一個選擇什麼?

回答

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



}