android中spinner如何实现省市二级联动
2个回答
展开全部
Androidspinner实现省市二级联动,需要自定义adpter,将数据源绑定到adpter,当点击省时,触发自定义响应事件,改变市,示例如下:
public class MainActivity extends Activity {
private TextView textView ;
private Spinner province;
private Spinner city;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//String[] provinces = new String[]{"-省份-","河北省","山西省","北京"};
textView = (TextView)this.findViewById(R.id.textView);
province = (Spinner)this.findViewById(R.id.province);
city = (Spinner)this.findViewById(R.id.city);
//(处理省的显示)
//将可选内容与ArrayAdapter的连接(从资源数组文件中获取数据)
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.province, android.R.layout.simple_spinner_item);
//new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, provinces);
//设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将数据绑定到Spinner视图上
province.setAdapter(adapter);
//添加条目被选中监听器
province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
//parent既是province对象
Spinner spinner = (Spinner)parent;
String pro = (String)spinner.getItemAtPosition(position);
//(处理省的市的显示)
//将默认值与ArrayAdapter连接(从资源数组文件中获取数据)
ArrayAdapter<CharSequence> cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.citydefault, android.R.layout.simple_spinner_item);
//new ArrayAdapter<CharSequence>
// (MainActivity.this,android.R.layout.simple_spinner_item, cities);
//获取所在省含有哪些市(从资源数组文件中获取数据)
if(pro.equals("河北省")){
cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.hb, android.R.layout.simple_spinner_item);
}else if(pro.equals("北京市")){
cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.bj, android.R.layout.simple_spinner_item);
}else if(pro.equals("山西省")){
cityAdapter = ArrayAdapter.createFromResource
(MainActivity.this, R.array.shx, android.R.layout.simple_spinner_item);
}
//绑定数据到Spinner(City)上
city.setAdapter(cityAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
Storm代理
2023-08-29 广告
2023-08-29 广告
StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,IP...
点击进入详情页
本回答由Storm代理提供
2014-04-11
展开全部
setOnItemSelectedListener
onItemSelected 看看选着哪个了 调用另外一个换数据
onItemSelected 看看选着哪个了 调用另外一个换数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询