安卓ListViw的自己笔记

本文介绍了一种基于XML的数据解析方法,并详细展示了如何利用Java进行XML文件的读取与解析,包括构建对象模型、实现数据分层管理和动态更新等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
			// 解析xml
			SAXParser 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;
	}
	@Override
	public void startDocument() throws SAXException {
		// 当读到第一个开始标签的时候,会触发这个方法
	}
	BoxCompany boxCompany = new BoxCompany();
	CarSystem carSystem = new CarSystem();
	CarModel carModel = new CarModel();
	BoxProtocol boxProtocol  = new BoxProtocol();
	
	@Override
	public 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));
		}
	}

	@Override
	public 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);
        } 
	}
	
	@Override
	public 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()){//做上次记录的判断,如果一样则修改后面一级的position
				position = getBoxProtocolExistPosition();
			}else{
				position = -1;
			}
		}
		lvBoxProtocolAdapter.setSelectItem(position);  
		lvBoxProtocolAdapter.notifyDataSetChanged();
		isFirstShowCarTypeFlag = false;
	}
5、listView的点击监听

public class CarTypeOnItemClickListener implements OnItemClickListener{

		@Override
		public 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();
				}
				
			}
			
		}
		
	}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值