2014-04-25 96 views
0

有兩個spinners,spinner1和spinner2,都有字符串陣列適配器作爲它們的適配器。應用程序的業務邏輯是,spinner2取決於spinner1的選定值。那麼如何從spinner1的選定項目過濾spinner2的數據呢? 例如適配器1已經〜應變陣列:如何篩選微調器的字符串陣列適配器?

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string-array name="region"> // for the label displayed 
     <item>Analamanga</item> 
     <item>Itasy</item> 
    </string-array> 
    <string-array name="region_id"> // this is for the actual value of the selected label 
     <item>1</item> 
     <item>2</item> 
    </string-array> 
</resources> 

適配器2具有字符串數組:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string-array name="district"> // for the label displayed 
     <item>Central</item> 
     <item>Boeny</item> 
    </string-array> 
    <string-array name="district_id"> // this is for the actual value of the selected label 
     <item>1</item> 
     <item>2</item> 
    </string-array> 
</resources> 
+0

您只需根據在Spinner 1上選擇的值篩選字符串數組2,並在Spinner2上設置一個新的適配器。 – joao2fast4u

+0

如何過濾第二個字符串數組? – pheromix

+0

這取決於。你在每個數組中有什麼值?你能舉一個例子嗎? – joao2fast4u

回答

2

使用一個HashTable<Integer,ArrayList<String>>映射您RegionSpinnerDistrictSpinner值。

HashTable將對RegionSpinner所選擇的項目,如按鍵和ArrayList<String>,供你DistrictSpinner適配器作爲值的位置。

然後,當你在RegionSpinner選擇一個項目,你設置DistrictSpinnerFilterSpinnerAdapter(擴展ArrayAdapterAdapter),所以它的值動態改變。

,將給予新的適配器ArrayList<String>將由您HashTable根據你RegionSpinner選擇的位置被退回。

下面的代碼:

public class SpinnerActivity extends Activity{ 

Spinner spinnerRegion, spinnerDistrict; 
int selectionCount=0; 
Hashtable<Integer,ArrayList<String>> spinnerValues; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_buttons_activity); 

    ArrayList<String> regions = new ArrayList<String>(); 
    regions.add("Analamanga"); 
    regions.add("Itasy"); 
    regions.add("ThirdRegion"); 

    ArrayList<String> analamangaDistricts = new ArrayList<String>(); 
    analamangaDistricts.add("Boeny"); 

    ArrayList<String> itasyDistricts = new ArrayList<String>(); 
    itasyDistricts.add("Central"); 

    ArrayList<String> thirdRegionDistricts = new ArrayList<String>(); 
    thirdRegionDistricts.add("District1"); 
    thirdRegionDistricts.add("District2"); 
    thirdRegionDistricts.add("District3"); 

    spinnerValues = new Hashtable<Integer, ArrayList<String>>(); 

    spinnerValues.put(0, analamangaDistricts); 
    spinnerValues.put(1, itasyDistricts); 
    spinnerValues.put(2, thirdRegionDistricts); 

    spinnerRegion = (Spinner) findViewById(R.id.spinner_region); 
    if(spinnerRegion != null) { 

     FilterSpinnerAdapter regionadapter = new FilterSpinnerAdapter(getApplicationContext(), R.layout.layout_spinner_item, regions); 

     regionadapter.setDropDownViewResource(R.layout.layout_simple_spinner_dropdown_item); 
     spinnerRegion.setAdapter(regionadapter); 
     spinnerRegion.setOnItemSelectedListener(new OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemId) { 
        FilterSpinnerAdapter newDistrictAdapter = new FilterSpinnerAdapter(getApplicationContext(), R.layout.layout_spinner_item, spinnerValues.get(position)); 
        newDistrictAdapter.setDropDownViewResource(R.layout.layout_simple_spinner_dropdown_item); 
        spinnerDistrict.setAdapter(newDistrictAdapter);   
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { } 
     }); 

    } 

    spinnerDistrict = (Spinner) findViewById(R.id.spinner_district); 
    if(spinnerDistrict != null) { 

     FilterSpinnerAdapter districtadapter = new FilterSpinnerAdapter(getApplicationContext(), R.layout.layout_spinner_item, analamangaDistricts); 

     districtadapter.setDropDownViewResource(R.layout.layout_simple_spinner_dropdown_item); 
     spinnerDistrict.setAdapter(districtadapter); 
     spinnerDistrict.setOnItemSelectedListener(new OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemId) { 
       //do whatever you want here 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { } 
     }); 

    } 
} 

public class FilterSpinnerAdapter extends ArrayAdapter<String> { 
    public FilterSpinnerAdapter(Context context, int resource, ArrayList<String> ys) { 
     super(context, resource, ys); 
    } 

    @Override 
    public int getCount() { 
     // - 1 so that the hint (last item) isn't shown 
     return super.getCount(); 
    } 

    @Override 
    public String getItem(int position) { 
     return super.getItem(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return super.getItemId(position); 
    } 

} 

}

更新: R.layout.layout_spinner_item是在離心機上每個DROP_DOWN項目的佈局。它包含一個簡單的TextView。 喜歡傾向:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/spinnerTitle" android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="5dp" android:textStyle="bold" /> 

我希望能對你有所幫助。 :D

+0

顯示兩個spinners的Activity的佈局是否爲'R.layout.layout_spinner_item'? – pheromix

+0

@pheromix我已經更新了我的答案。 – joao2fast4u

相關問題