2014-04-25 66 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

相关问题