安卓ListViw的自己笔记
来源:互联网 发布:学大汉武立国 知乎 编辑:程序博客网 时间:2024/06/05 14:31
1、xml数据文件
<root> <carcompany name="aa"> <carsystem name="bb"> <cardemol name="cc"> <carprotocol name="dd"/><carprotocol name="dd"/> </cardemol> <cardemol name="cc2"> <carprotocol name="dd"/> <carprotocol name="dd2"/><carprotocol name="dd3"/> </cardemol> <cardemol name="cc3"> <carprotocol name="dd"/><carprotocol name="dd2"/> </cardemol> </carsystem> </carcompany></root>
2、xml数据解析
public static String[] mBoxCompanyDatas;public static Map<String, String[]> mCarSystemDatasMap = new HashMap<String, String[]>();public static Map<String, String[]> mCarModelDatasMap = new HashMap<String, String[]>();public static Map<String, String[]> mBoxProDatasMap = new HashMap<String, String[]>();public static String mCurrentBoxCompanyName;public static String mCurrentCarSystemName;public static String mCurrentCarModelName ="";public static String mCurrentBoxProName = "";public static String mCurrentCarTypeName = "";
protected void initCarTypeDatas(){ List<BoxCompany> boxCompanyList = null; AssetManager asset = getAssets(); try { InputStream input; if(isZh()){ input = asset.open("cartype_zh_data.xml"); }else{ input = asset.open("cartype_data.xml"); }// 创建一个解析xml的工厂对象SAXParserFactory spf = SAXParserFactory.newInstance();// 解析xmlSAXParser parser = spf.newSAXParser();XmlParserHandler handler = new XmlParserHandler();parser.parse(input, handler);input.close();// 获取解析出来的数据boxCompanyList = handler.getDataList();if (boxCompanyList!= null && !boxCompanyList.isEmpty()) {mCurrentBoxCompanyName = boxCompanyList.get(0).getName();List<CarSystem> cityList = boxCompanyList.get(0).getSysList();if (cityList!= null && !cityList.isEmpty()) {mCurrentCarSystemName = cityList.get(0).getName();List<CarModel> districtList = cityList.get(0).getModList();mCurrentCarModelName = districtList.get(0).getName();List<BoxProtocol> villageList = districtList.get(0).getProList();if(villageList!=null&&!villageList.isEmpty()){mCurrentBoxProName = villageList.get(0).getName();}}}mBoxCompanyDatas = new String[boxCompanyList.size()]; for (int i=0; i< boxCompanyList.size(); i++) { mBoxCompanyDatas[i] = boxCompanyList.get(i).getName(); List<CarSystem> carSystemList = boxCompanyList.get(i).getSysList(); String[] carSystemNames = new String[carSystemList.size()]; for (int j=0; j< carSystemList.size(); j++) { carSystemNames[j] = carSystemList.get(j).getName(); List<CarModel> carModelList = carSystemList.get(j).getModList(); String[] carModelNameArray = new String[carModelList.size()]; for (int k=0; k<carModelList.size(); k++) { carModelNameArray[k] = carModelList.get(k).getName(); List<BoxProtocol> boxProList = carModelList.get(k).getProList(); String[] boxProNames = new String[boxProList.size()]; for(int m=0;m<boxProList.size();m++){ BoxProtocol boxProtocol = new BoxProtocol(boxProList.get(m).getName()); boxProNames[m] = boxProtocol.getName(); } mBoxProDatasMap.put(carModelNameArray[k], boxProNames); } mCarModelDatasMap.put(carSystemNames[j], carModelNameArray); } mCarSystemDatasMap.put(boxCompanyList.get(i).getName(), carSystemNames); }} catch (Exception e) {e.printStackTrace();}}
/** * 判断是否是中文的语言环境 * @return true 是;false 否 */private boolean isZh() { Locale locale = getResources().getConfiguration().locale; String language = locale.getLanguage(); if (language.endsWith("zh")) return true; else return false; }3、XmlParserHandler文件
public class XmlParserHandler extends DefaultHandler {/** * 存储所有的解析对象 */private List<BoxCompany> boxCompanyList = new ArrayList<BoxCompany>(); public XmlParserHandler() {}public List<BoxCompany> getDataList() {return boxCompanyList;}@Overridepublic void startDocument() throws SAXException {// 当读到第一个开始标签的时候,会触发这个方法}BoxCompany boxCompany = new BoxCompany();CarSystem carSystem = new CarSystem();CarModel carModel = new CarModel();BoxProtocol boxProtocol = new BoxProtocol();@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {// 当遇到开始标记的时候,调用这个方法if (qName.equals("carcompany")) {boxCompany = new BoxCompany();boxCompany.setName(attributes.getValue(0));boxCompany.setSysList(new ArrayList<CarSystem>());} else if (qName.equals("carsystem")) {carSystem = new CarSystem();carSystem.setName(attributes.getValue(0));carSystem.setModList(new ArrayList<CarModel>());} else if (qName.equals("cardemol")) {carModel = new CarModel();carModel.setName(attributes.getValue(0));carModel.setProList(new ArrayList<BoxProtocol>());} else if(qName.equals("carprotocol")){boxProtocol = new BoxProtocol();boxProtocol.setName(attributes.getValue(0));}}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {// 遇到结束标记的时候,会调用这个方法if(qName.equals("carprotocol")){ carModel.getProList().add(boxProtocol); }else if (qName.equals("cardemol")) {carSystem.getModList().add(carModel); } else if (qName.equals("carsystem")) { boxCompany.getSysList().add(carSystem); } else if (qName.equals("carcompany")) { boxCompanyList.add(boxCompany); } }@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {}}4、主要使用中的逻辑操作
private void updateBoxCompanyDates(){lvBoxCompanyAdapter = new LvBoxCompanyAdapter(CarSetApplication.mBoxCompanyDatas, mContext);lv_box_company.setAdapter(lvBoxCompanyAdapter);int position = carTypeSp.getInt(BOXCOMPANYPOSNAME, POSDEL);lvBoxCompanyAdapter.setSelectItem(position); lvBoxCompanyAdapter.notifyDataSetChanged();updateCarSystemDates();}
private void updateCarSystemDates(){int pCurrent = lvBoxCompanyAdapter.getSelectItem();if(pCurrent!=-1){CarSetApplication.mCurrentBoxCompanyName = CarSetApplication.mBoxCompanyDatas[pCurrent];}else{CarSetApplication.mCurrentBoxCompanyName = CarSetApplication.mBoxCompanyDatas[0];}String[] carsystemdates = CarSetApplication.mCarSystemDatasMap.get(CarSetApplication.mCurrentBoxCompanyName);if (carsystemdates == null) {carsystemdates = new String[] { "" };}lvCarSystemAdapter = new LvCarSystemAdapter(carsystemdates, mContext);lv_box_system.setAdapter(lvCarSystemAdapter);int position;if(isFirstShowCarTypeFlag){position = carTypeSp.getInt(CARSYSTEMPOSNAME, POSDEL);}else{position = POSDEL;}lvCarSystemAdapter.setSelectItem(position); lvCarSystemAdapter.notifyDataSetChanged();updateCarModelDates();}
private void updateCarModelDates(){int pCurrent = lvCarSystemAdapter.getSelectItem();if(pCurrent!=-1){CarSetApplication.mCurrentCarSystemName = CarSetApplication.mCarSystemDatasMap.get(CarSetApplication.mCurrentBoxCompanyName)[pCurrent];}else{CarSetApplication.mCurrentCarSystemName = CarSetApplication.mCarSystemDatasMap.get(CarSetApplication.mCurrentBoxCompanyName)[0];}String[] carsystemdates = CarSetApplication.mCarModelDatasMap.get(CarSetApplication.mCurrentCarSystemName);if (carsystemdates == null) {carsystemdates = new String[] { "" };}lvCarModelAdapter = new LvCarSystemAdapter(carsystemdates, mContext);lv_box_model.setAdapter(lvCarModelAdapter);int position;if(isFirstShowCarTypeFlag){position = carTypeSp.getInt(CARMODELPOSNAME, POSDEL);}else{position = POSDEL;}lvCarModelAdapter.setSelectItem(position); lvCarModelAdapter.notifyDataSetChanged();updateBoxProtocolDates(); }
private void updateBoxProtocolDates(){int pCurrent = lvCarModelAdapter.getSelectItem();if(pCurrent!=-1){CarSetApplication.mCurrentCarModelName = CarSetApplication.mCarModelDatasMap.get(CarSetApplication.mCurrentCarSystemName)[pCurrent];}else{CarSetApplication.mCurrentCarModelName = CarSetApplication.mCarModelDatasMap.get(CarSetApplication.mCurrentCarSystemName)[0];}tv_carModel.setText(CarSetApplication.mCurrentCarModelName);String[] boxprotocoldates = CarSetApplication.mBoxProDatasMap.get(CarSetApplication.mCurrentCarModelName);if (boxprotocoldates == null) {boxprotocoldates = new String[] { "" };}lvBoxProtocolAdapter = new LvBoxProtocolAdapter(boxprotocoldates, mContext);lv_box_protocol.setAdapter(lvBoxProtocolAdapter);int position;if(isFirstShowCarTypeFlag){position = carTypeSp.getInt(BOXPROTOCOLPOSNAME, -1);}else{if(isExistBoxProtocol()){//做上次记录的判断,如果一样则修改后面一级的positionposition = getBoxProtocolExistPosition();}else{position = -1;}}lvBoxProtocolAdapter.setSelectItem(position); lvBoxProtocolAdapter.notifyDataSetChanged();isFirstShowCarTypeFlag = false;}5、listView的点击监听
public class CarTypeOnItemClickListener implements OnItemClickListener{@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {if(parent == lv_box_company){if(position!=lvBoxCompanyAdapter.getSelectItem()){lvBoxCompanyAdapter.setSelectItem(position); lvBoxCompanyAdapter.notifyDataSetChanged();updateCarSystemDates();}}else if(parent == lv_box_system){if(position!=lvCarSystemAdapter.getSelectItem()){lvCarSystemAdapter.setSelectItem(position); lvCarSystemAdapter.notifyDataSetChanged();updateCarModelDates();}}else if(parent == lv_box_model){if(position!=lvCarModelAdapter.getSelectItem()){lvCarModelAdapter.setSelectItem(position); lvCarModelAdapter.notifyDataSetChanged();updateBoxProtocolDates();}}else if(parent == lv_box_protocol){CarSetApplication.mCurrentBoxProName = CarSetApplication.mBoxProDatasMap.get(CarSetApplication.mCurrentCarModelName)[position];//TODO 加载相应的mcu升级文件if(checkMcuFileExist()){//如果文件存在if(position!=lvBoxProtocolAdapter.getSelectItem()){lvBoxProtocolAdapter.setSelectItem(position); lvBoxProtocolAdapter.notifyDataSetChanged();//保存当前四个ListView的选择的position,在下次初始化的需要的参数box_company_selecter_position = lvBoxCompanyAdapter.getSelectItem();car_system_selecter_position = lvCarSystemAdapter.getSelectItem();car_model_selecter_position = lvCarModelAdapter.getSelectItem();box_protocol_selecter_position = lvBoxProtocolAdapter.getSelectItem();carTypeEditor.putInt(BOXCOMPANYPOSNAME, box_company_selecter_position);carTypeEditor.putInt(CARSYSTEMPOSNAME, car_system_selecter_position);carTypeEditor.putInt(CARMODELPOSNAME, car_model_selecter_position);carTypeEditor.putInt(BOXPROTOCOLPOSNAME, box_protocol_selecter_position);carTypeEditor.putString(MCURRENTCARMODELNAME, CarSetApplication.mCurrentCarModelName);setCurrentCarTypeName();carTypeEditor.putString(MCURRENTCARTYPENAME, CarSetApplication.mCurrentCarTypeName);carTypeEditor.commit();}carTypeDialog.dismiss();//TODO 改变Mcu相应的界面}else{//如果文件不存在carTypeDialog.dismiss();showNoMcuFileDialog();}}}}
阅读全文
0 0
- 安卓ListViw的自己笔记
- Android学习笔记 优化listview 获取listviw中item的内容 获取应用中的activity信息等
- 安卓的提高,测测自己的安卓水平
- 自己编的安卓游戏
- 自己写的安卓简易浏览器
- 打造属于自己的安卓menu
- 献给初学安卓的自己。。Android
- 打造自己的安卓Metro界面
- 自己写的异步下载 安卓
- 安卓相机自己个的感想
- 安卓自己遇到过的问题
- 安卓学习笔记汇总----极客学院安卓教学视频观看所做的笔记和自己的理解
- ListViw-解析
- 自己总结的一些安卓的实用小技巧
- 自己整理的js调用安卓Java的方法
- 安卓学习笔记0安卓环境的搭建
- 自己写的安卓程序如何改变图标
- 安卓应用开发之定义自己的权限
- echars 生成多张图片
- noip2013转圈游戏
- 几种向数组中添加元素方法的性能比较
- Codeforces895A. Pizza Separation
- Makefile --使用条件判断
- 安卓ListViw的自己笔记
- tomcat配置
- Mac 终端命令大全
- Linux下通过命令行创建和设置一个MySQL用户
- PBR Step by Step(三)BRDFs
- Linux常用命令
- 实战:抓取某只基金历史净值数据
- fortran 002 稍微复杂一点的输入(时间序列读取)
- 在IDEA中使用MyBatis Generator逆向工程生成代码