当前位置:操作系统 > 安卓/Android >>

两个spinner的联动,省会城市

//////////////////////////////////////////////////////////////////////////
   shengfenSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
 
   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    
    shengfenValue = (String) shengfenSpinner.getItemAtPosition(arg2);
    updateSpiner(shengfenValue);
   }
 
   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    shengfenSpinner.setSelection(0);
   }
  });
 
   citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
 
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
     // TODO Auto-generated method stub
     
     cityValue = (String) citySpinner.getItemAtPosition(arg2);
    // Log.i("aa",""+cityValue);
     
    }
 
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
     // TODO Auto-generated method stub
     citySpinner.setSelection(0);
    }
   });
private void updateSpiner(String city)
   {
    if(city.equalsIgnoreCase("北京"))
       {
      
           ArrayAdapter<CharSequence> cityadapter = ArrayAdapter.createFromResource(this, R.array.beijing_city, R.layout.spinner_item);
           cityadapter.setDropDownViewResource(R.layout.spinner_down_item);
 
       citySpinner.setAdapter(cityadapter);
       }
    
     if(city.equalsIgnoreCase("天津"))
     {
    
       ArrayAdapter<CharSequence> cityadapter = ArrayAdapter.createFromResource(this, R.array.tianjin_city, R.layout.spinner_item);
       cityadapter.setDropDownViewResource(R.layout.spinner_down_item);
 
  citySpinner.setAdapter(cityadapter);
     }
   }
Java代码 
<resources> 
    <!-- Used in View/Spinner1.java --> 
    <string-array name="shengfen"> 
        <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> 
        <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> 
        <item>澳门</item> 
    </string-array> 
     
    
    <string-array name="beijing_city"> 
        <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="tianjin_city"> 
   &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,