安卓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();}}}}








原创粉丝点击