有两个微调器,比如spinner1和spinner2,它们都有两个字符串数组适配器作为它们的适配器。应用程序的业务逻辑是spinner2依赖于spinner1的选定值。那么如何从spinner1的选定项中过滤spinner2的数据? 例如,适配器1具有strin-array:
<?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 :(得分:2)
使用HashTable<Integer,ArrayList<String>>
映射您的RegionSpinner
和DistrictSpinner
值。
此HashTable
将RegionSpinner
上所选项目的位置作为关键字,ArrayList<String>
将DistrictSpinner
适配器的位置作为值。
然后,当您在RegionSpinner
上选择一个项目时,您会在FilterSpinnerAdapter
上设置一个新的Adapter
(ArrayAdapter
扩展DistrictSpinner
),所以它上面的值动态变化。
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);
}
}
。
以下是代码:
TextView
}
更新:
R.layout.layout_spinner_item是微调器上每个drop_down项的布局。它包含一个简单的<?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" />
。
像这样:
{{1}}
我希望能帮到你。 :d