以前做過,數(shù)據(jù)是在本地xml里面取的:上面是省市區(qū)的xml數(shù)據(jù)網(wǎng)盤地址。 如果要做成從服務(wù)器取,那么就一次性取下來,然后還是一樣的流程。如果你想選擇省再去取對(duì)應(yīng)的市,那么就要傳省的id去獲取對(duì)應(yīng)的市,這時(shí)候就要有等待框,可能用戶體驗(yàn)差一點(diǎn)。
成都創(chuàng)新互聯(lián)是一家以網(wǎng)站建設(shè)公司、網(wǎng)頁(yè)設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、成都網(wǎng)站營(yíng)銷、小程序App開發(fā)等移動(dòng)開發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為成都陽(yáng)光房等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。
public class Area {
private String province;
private String city;
//省略屬性區(qū)和get set方法
public boolean isSame(Area area){
if(!area.getProvince().equals(province)){
return false;
}
if(!area.getCity().equals(province)){
return false;
}
//省略 區(qū)
return true;//相同
}
}
Androidspinner實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng),需要自定義adpter,將數(shù)據(jù)源綁定到adpter,當(dāng)點(diǎn)擊省時(shí),觸發(fā)自定義響應(yīng)事件,改變市,示例如下:
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);?
//(處理省的顯示)?
//將可選內(nèi)容與ArrayAdapter的連接(從資源數(shù)組文件中獲取數(shù)據(jù))?
ArrayAdapterCharSequence?adapter?=?
ArrayAdapter.createFromResource(this,?R.array.province,?android.R.layout.simple_spinner_item);?
//new?ArrayAdapterString(this,android.R.layout.simple_spinner_item,?provinces);?
//設(shè)置下拉列表的風(fēng)格?
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);?
//將數(shù)據(jù)綁定到Spinner視圖上?
province.setAdapter(adapter);?
//添加條目被選中監(jiān)聽器?
province.setOnItemSelectedListener(new?AdapterView.OnItemSelectedListener()?{?
@Override?
public?void?onItemSelected(AdapterView??parent,?View?view,int?position,?long?id)?{?
//parent既是province對(duì)象?
Spinner?spinner?=?(Spinner)parent;?
String?pro?=??(String)spinner.getItemAtPosition(position);?
//(處理省的市的顯示)?
//將默認(rèn)值與ArrayAdapter連接(從資源數(shù)組文件中獲取數(shù)據(jù))?
ArrayAdapterCharSequence?cityAdapter?=?ArrayAdapter.createFromResource?
(MainActivity.this,?R.array.citydefault,?android.R.layout.simple_spinner_item);?
//new??ArrayAdapterCharSequence?
//???????????(MainActivity.this,android.R.layout.simple_spinner_item,?cities);?
//獲取所在省含有哪些市(從資源數(shù)組文件中獲取數(shù)據(jù))?
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);?
}?
//綁定數(shù)據(jù)到Spinner(City)上?
city.setAdapter(cityAdapter);?
}?
@Override?
public?void?onNothingSelected(AdapterView??parent)?{?
}?
});?
}?
}
標(biāo)題名稱:android省市,安卓是哪個(gè)省
轉(zhuǎn)載源于:http://jinyejixie.com/article8/dssehip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、定制網(wǎng)站、App設(shè)計(jì)、網(wǎng)站維護(hù)、面包屑導(dǎo)航、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)