通常我们查询的范围比较大时都习惯联查,而有时我们在输入的时候为了方便采用级联,这样我们输入的内容仅仅靠选择就行了。
比如查询某地的相关信息我们需输入省市县(这里只设置了省和市),如图:
问题来了,不知哪里出错,不仅省和市连不上,市里面的选择内容都没了,无奈……
大家帮忙看看代码哪里有问题:
public class MarketActivity extends Activity {
Spinner spinnerprovince;
Spinner spinnercity;
protected String provinceValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_market);
// 初始化控件
spinnerprovince = (Spinner) findViewById(R.id.province);
spinnercity = (Spinner) findViewById(R.id.city);
// 适配器
ArrayAdapter<CharSequence> _Adapter = ArrayAdapter.createFromResource(
this, R.array.spinnerprovince,
android.R.layout.simple_spinner_item);
// 数据绑定到适配器
_Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerprovince.setAdapter(_Adapter);
spinnerprovince
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
provinceValue = (String) spinnerprovince
.getItemAtPosition(arg2);
updataSpinner(provinceValue);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
spinnerprovince.setSelection(0);
}
});
spinnercity
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
spinnercity.setSelection(0);
}
});
}
private void updataSpinner(String provinceValue) {
if (spinnerprovince.equals("山东省")) {
ArrayAdapter<CharSequence> _bAdapter = ArrayAdapter
.createFromResource(this, R.array.cityofshandong,
android.R.layout.simple_spinner_item);
_bAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercity.setAdapter(_bAdapter);
}
if (spinnerprovince.equals("山西省")) {
ArrayAdapter<CharSequence> _cAdapter = ArrayAdapter
.createFromResource(this, R.array.cityofshanxi,
android.R.layout.simple_spinner_item);
_cAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercity.setAdapter(_cAdapter);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
其他数据都写在xml里了,也不会出错啊?
xml:
<resources>
<string-array name="spinnerprovince">
<item>山东省</item>
<item>山西省</item>
<item>安徽省</item>
<item>河北省</item>
<item>陕西省</item>
<item>湖南省</item>
</string-array>
<string-array name="cityofshandong">
<item>临沂市</item>
<item>济南市</item>
<item>济宁市</item>
<item>潍坊市</item>
<item >青岛市</item>
<item >淄博市</item>
<item >枣庄市</item>
<item >烟台市</item>
<item >泰安市</item>
<item >威海市</item>
<item >日照市</item>
<item >滨州市</item>
<item >德州市</item>
<item >聊城市</item>
<item >菏泽市</item>
<item >莱芜市</item>
<item >东营市</item>
</string-array>
<string-array name="cityofshanxi">
<item >太原市</item>
<item >古交市</item>
<item >潞城市</item>
<item >高平市</item>
<item >介休市</item>
<item >河津市</item>
<item >忻州市</item>
<item >原平市</item>
<item >临汾市</item>
<item >吕梁市</item>
<item >孝义市</item>
<item >汾阳市</item>
</string-array>
</resources>
我想选中山东省的时候取cityofshandong里的数据,选中山西省的时候取cityofshanxi里面的数据。可是都没有 大家快来帮忙啊