实现的效果图如下
其中 难点在于 xml 的解析,以及两个listview 的交互
解析xml
public class XmlParserHandler extends DefaultHandler { /** * 存储所有的解析对象 */ private List<ProvinceModel> provinceList = new ArrayList<ProvinceModel>(); public XmlParserHandler() { } public List<ProvinceModel> getDataList() { return provinceList; } @Override public void startDocument() throws SAXException { // 当读到第一个开始标签的时候,会触发这个方法 } ProvinceModel provinceModel = new ProvinceModel(); CityModel cityModel = new CityModel(); @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)); } } @Override public void endElement(String uri,String qName) throws SAXException { // 遇到结束标记的时候,会调用这个方法 if (qName.equals("city")) { provinceModel.getCityList().add(cityModel); } else if (qName.equals("province")) { provinceList.add(provinceModel); } } @Override public void characters(char[] ch,int start,int length) throws SAXException { } }
两个listView 数据交互
{CSDN:CODE:
mainlist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,
int position,long id) {
selectSE=position;
initAdapter(mCitisDatasMap.get(mProvinceDatas[position]));
mainAdapter.setSelectItem(position);
mainAdapter.notifyDataSetChanged();
}
});
morelist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,long id) {
//Toast.makeText(context.getActivity(),mProvinceDatas[selectSE]+mCitisDatasMap.get(mProvinceDatas[selectSE])[position],0).show();
moreAdapter.setSelectItem(position);
moreAdapter.notifyDataSetChanged();
context.setCityName(mCitisDatasMap.get(mProvinceDatas[selectSE])[position]);
SelectCity.this.dismiss();
}
});
}
mainlist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,
int position,long id) {
selectSE=position;
initAdapter(mCitisDatasMap.get(mProvinceDatas[position]));
mainAdapter.setSelectItem(position);
mainAdapter.notifyDataSetChanged();
}
});
morelist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,long id) {
//Toast.makeText(context.getActivity(),mProvinceDatas[selectSE]+mCitisDatasMap.get(mProvinceDatas[selectSE])[position],0).show();
moreAdapter.setSelectItem(position);
moreAdapter.notifyDataSetChanged();
context.setCityName(mCitisDatasMap.get(mProvinceDatas[selectSE])[position]);
SelectCity.this.dismiss();
}
});
}