收货地址
一.三级联动PickerView
①.文件
<root>
<province name="安徽省">
<city name="安庆市">
<district name="枞阳县" zipcode="246000" />
<district name="大观区" zipcode="246000" />
<district name="怀宁县" zipcode="246000" />
<district name="潜山县" zipcode="246000" />
<district name="宿松县" zipcode="246000" />
<district name="太湖县" zipcode="246000" />
<district name="桐城市" zipcode="246000" />
<district name="望江县" zipcode="246000" />
<district name="宜秀区" zipcode="246000" />
<district name="迎江区" zipcode="246000" />
<district name="岳西县" zipcode="246000" />
<district name="其他" zipcode="246000" />
</city>
<city name="蚌埠市">
<district name="蚌山区" zipcode="233000" />
<district name="固镇县" zipcode="233000" />
<district name="怀远县" zipcode="233000" />
<district name="淮上区" zipcode="233000" />
<district name="龙子湖区" zipcode="233000" />
<district name="五河县" zipcode="233000" />
<district name="禹会区" zipcode="233000" />
<district name="其他" zipcode="233000" />
</city>
<city name="亳州市">
<district name="利辛县" zipcode="236000" />
<district name="蒙城县" zipcode="236000" />
<district name="谯城区" zipcode="236000" />
<district name="涡阳县" zipcode="236000" />
<district name="其他" zipcode="236000" />
</city>
②.省
public class ProvinceModel {
private String name;
private List<CityModel> cityList;
public ProvinceModel() {
super();
}
public ProvinceModel(String name, List<CityModel> cityList) {
super();
this.name = name;
this.cityList = cityList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<CityModel> getCityList() {
return cityList;
}
public void setCityList(List<CityModel> cityList) {
this.cityList = cityList;
}
@Override
public String toString() {
// return "ProvinceModel [name=" + name + ", cityList=" + cityList + "]";
return name;
}
}
③。市
public class CityModel {
private String name;
private List<DistrictModel> districtList;
public CityModel() {
super();
}
public CityModel(String name, List<DistrictModel> districtList) {
super();
this.name = name;
this.districtList = districtList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<DistrictModel> getDistrictList() {
return districtList;
}
public void setDistrictList(List<DistrictModel> districtList) {
this.districtList = districtList;
}
@Override
public String toString() {
return "CityModel [name=" + name + ", districtList=" + districtList
+ "]";
}
}
④.区
public class DistrictModel {
private String name;
private String zipcode;
public DistrictModel() {
super();
}
public DistrictModel(String name, String zipcode) {
super();
this.name = name;
this.zipcode = zipcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
@Override
public String toString() {
return "DistrictModel [name=" + name + ", zipcode=" + zipcode + "]";
}
}
⑥.sax解析xml数据
public class XmlParserHandler extends DefaultHandler {
/**
* 存储所有的解析对象
*/
private List<ProvinceModel> provinceList = new ArrayList<ProvinceModel>();
ProvinceModel provinceModel = new ProvinceModel();
CityModel cityModel = new CityModel();
DistrictModel districtModel = new DistrictModel();
public XmlParserHandler() {
}
//获取所有解析对象
public List<ProvinceModel> getDataList() {
return provinceList;
}
//1.start开始解析
@Override
public void startDocument() throws SAXException {
// 当读到第一个开始标签的时候,会触发这个方法
}
//2.解析节点
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// 当遇到开始标记的时候,调用这个方法
if (qName.equals("province")) {
provinceModel = new ProvinceModel();
provinceModel.setName(attributes.getValue(0));
provinceModel.setCityList(new ArrayList<CityModel>());
} else if (qName.equals("city")) {
cityModel = new CityModel();
cityModel.setName(attributes.getValue(0));
cityModel.setDistrictList(new ArrayList<DistrictModel>());
} else if (qName.equals("district")) {
districtModel = new DistrictModel();
districtModel.setName(attributes.getValue(0));
districtModel.setZipcode(attributes.getValue(1));
}
}
//3.解析节点结束
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// 遇到结束标记的时候,会调用这个方法
if (qName.equals("district")) {
cityModel.getDistrictList().add(districtModel);
} else if (qName.equals("city")) {
provinceModel.getCityList().add(cityModel);
} else if (qName.equals("province")) {
provinceList.add(provinceModel);
}
}
//4.获取节点的内容
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
}
//5.start结束解析
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
}
⑦.收货地址设置post给服务器
/**
* Created by asus on 2016/11/2.
*/
public class AddressAddActivity extends AppCompatActivity {
@ViewInject(R.id.edittxt_consignee)//收件人姓名
private ClearEditText mEditConsignee;
@ViewInject(R.id.edittxt_phone)//收件人电话
private ClearEditText mEditPhone;
@ViewInject(R.id.edittxt_add)//收件人详细地址
private ClearEditText mEditAddr;
@ViewInject(R.id.txt_address)//联系人地址
private TextView mTxtAddress;
@ViewInject(R.id.toolbar)
private CnToolbar mToolBar;
private OptionsPickerView pvOptions;
private OkHttpHelper mHttpHelper=OkHttpHelper.getInstance();
private List<ProvinceModel> mProvinces;//省
private ArrayList<ArrayList<String>> mCities = new ArrayList<ArrayList<String>>();//市
private ArrayList<ArrayList<ArrayList<String>>> mDistricts = new ArrayList<ArrayList<ArrayList<String>>>();//县
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address_add);
ViewUtils.inject(this);
initToolbar();
init();
}
private void initToolbar() {
mToolBar.setRightButtonText("完成");
mToolBar.setmRightImageButtonOnclickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createAddress();
}
});
}
private void init() {
initProvinceDatas();
//选项选择器
pvOptions = new OptionsPickerView(this);
pvOptions.setPicker((ArrayList) mProvinces,mCities,mDistricts,true);
pvOptions.setTitle("选择城市");
pvOptions.setCyclic(false,false,false);//设置是否可以循环滚动
pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int option2, int options3) {
String tx = mProvinces.get(options1).getName() +" "
+ mCities.get(options1).get(option2) +" "
+ mDistricts.get(options1).get(option2).get(options3);
mTxtAddress.setText(tx);
}
});
}
//sax解析xml数据
private void initProvinceDatas() {
AssetManager assets = getAssets();
InputStream inputStream = null;
try {
inputStream = assets.open("province_data.xml");
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser = saxParserFactory.newSAXParser();
MyxmlParserHandler myxmlParserHandler = new MyxmlParserHandler();
parser.parse(inputStream,myxmlParserHandler);
// 获取解析出来的数据
mProvinces = myxmlParserHandler.getDataList();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//填充数据
if(mProvinces!=null){
for (ProvinceModel p :mProvinces){
List<CityModel> cityList = p.getCityList();//1.城市对象集合 17
ArrayList<String> cityStrs = new ArrayList<>(); //城市String List
for (CityModel c :cityList){
cityStrs.add(c.getName()); //1
ArrayList<ArrayList<String>> dts = new ArrayList<>(); // 地区String List1 11
List<DistrictModel> districts = c.getDistrictList();//2.地区对象集合 12
ArrayList<String> districtStrs = new ArrayList<>();//地区String List2 12
for (DistrictModel d : districts){
districtStrs.add(d.getName()); // 把城市名称放入 districtStrs
}
dts.add(districtStrs);
mDistricts.add(dts);
}
LogUtils.d("地区大小 1"+mDistricts.size());
mCities.add(cityStrs); // 组装城市数据
}
LogUtils.d("城市List集合 2"+mCities.size());
}
}
//显示三级联动
@OnClick(R.id.ll_city_picker)
public void showCityPickerView(View view){
pvOptions.show();
}
//将数据post给服务器
public void createAddress(){
String consignee = mEditConsignee.getText().toString();
String phone = mEditPhone.getText().toString();
String address = mTxtAddress.getText().toString() + mEditAddr.getText().toString();
HashMap<String,Object> params = new HashMap<>(1);
params.put("user_id",MyApplication.getInstance().getUser().getId());
params.put("consignee",consignee);
params.put("phone",phone);
params.put("addr",address);
params.put("zip_code","000000");
mHttpHelper.post(Contants.API.ADDRESS_CREATE, params, new SpotsCallBack<BaseRespMsg>(this,false){
@Override
public void onSuccess(Response response, BaseRespMsg baseRespMsg) {
if(baseRespMsg.getStatus()==BaseRespMsg.STATUS_SUCCESS){
setResult(RESULT_OK);
finish();
}
}
@Override
public void onError(Response response, int code, Exception e) {
}
});
}
}
/**
* Created by asus on 2016/11/2.
*/
public class AddressListActivity extends AppCompatActivity {
@ViewInject(R.id.toolbar)
private CnToolbar mToolBar;
@ViewInject(R.id.recycler_view)
private RecyclerView mRecyclerview;
private AddressAdapter mAdapter;
private OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address_list);
ViewUtils.inject(this);
initToolbar();
initAddress();
}
private void initToolbar() {
mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
mToolBar.setRightButtonIcon(getResources().getDrawable(R.drawable.icon_add));
mToolBar.setmRightImageButtonOnclickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AddressListActivity.this, AddressAddActivity.class);
startActivityForResult(intent, Contants.REQUEST_CODE);
}
});
}
//请求数据
private void initAddress() {
HashMap<String, Object> params = new HashMap<>(1);
params.put("user_id", MyApplication.getInstance().getUser().getId());
mHttpHelper.get(Contants.API.ADDRESS_LIST, params, new SpotsCallBack<List<Address>>(this, false) {
@Override
public void onSuccess(Response response, List<Address> addresses) {
showAddress(addresses);//展示地址信息
}
@Override
public void onError(Response response, int code, Exception e) {
}
});
}
//展示数据
private void showAddress(List<Address> addresses) {
Collections.sort(addresses);//按照地址进行排序
if (mAdapter == null) {
mAdapter = new AddressAdapter(addresses, this);
mAdapter.setOnAddressLisneter(new AddressAdapter.OnAddressLisneter() {
@Override
public void setDefault(Address address) {
//监听checkbox的点击事件 点击之后更新数据,提交到服务器
updateAddress(address);
}
@Override
public void deleteAddress(Address address) {
todeleteAddress(address);
}
});
mRecyclerview.setAdapter(mAdapter);
mRecyclerview.setLayoutManager(new LinearLayoutManager(AddressListActivity.this));
mRecyclerview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
} else {
mAdapter.refreshData(addresses);
mRecyclerview.setAdapter(mAdapter);
}
}
//删除地址
private void todeleteAddress(Address address) {
HashMap<String, Object> params = new HashMap<>(1);
params.put("id", address.getId());
mHttpHelper.post(Contants.API.ADDRESS_DEL, params, new SpotsCallBack<BaseRespMsg>(this, false) {
@Override
public void onSuccess(Response response, BaseRespMsg baseRespMsg) {
if (baseRespMsg.getStatus() == BaseRespMsg.STATUS_SUCCESS) {
initAddress();
}
}
@Override
public void onError(Response response, int code, Exception e) {
}
});
}
//更新地址数据
public void updateAddress(Address address) {
HashMap<String, Object> params = new HashMap<>(1);
params.put("id", address.getId());
params.put("consignee", address.getConsignee());
params.put("phone", address.getPhone());
params.put("addr", address.getAddr());
params.put("zip_code", address.getZipCode());
params.put("is_default", address.getIsDefault());
mHttpHelper.post(Contants.API.ADDRESS_UPDATE, params, new SpotsCallBack<BaseRespMsg>(this, false) {
@Override
public void onSuccess(Response response, BaseRespMsg baseRespMsg) {
if (baseRespMsg.getStatus() == BaseRespMsg.STATUS_SUCCESS) {
initAddress();
}
}
@Override
public void onError(Response response, int code, Exception e) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//当增加了数据的时候再重新请求一次
initAddress();
}
}