标签:== target blog where 文件大小 出错 amp .net class
近期在实现收货地址功能。用到了省市区三级联动效果。网上找到一般都是xml或json,数据源陈旧改动麻烦,改动了一下使用数据库方式实现了一下
数据源解决,因为数据量比較大通过初始化批量运行SQL的方式不合适,费时不说还easy出错,我採用的是在电脑上创建好数据库然后把数据库文件放到程序里边,通过以下这种方法能够把db文件复制到随意文件夹下。demo中我把文件放在了raw文件夹下
/** * * @param inStream * @param fileNme 文件名称 * @param newPath 要拷贝到的目录路径 */ public void copyFile(InputStream inStream,String fileNme, String newPath) { try { int bytesum = 0; int byteread = 0; File file = new File(newPath); //保证目录存在 if (!file.exists()) { file.mkdir(); } //假设文件存在覆盖 File newFile=new File(newPath+File.separator+fileNme); if(newFile.exists()){ newFile.delete(); newFile.createNewFile(); } FileOutputStream fs = new FileOutputStream(newFile); byte[] buffer = new byte[1024 * 2]; int length; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字节数 文件大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); fs.close(); } catch (Exception e) { System.out.println("拷贝文件操作出错"); e.printStackTrace(); } }
有了数据库文件仅仅须要通过下面方法就能够对该数据库做操作了,该方法返回的是给定数据库的引用
/** * 打开数据库文件 * @return */ public SQLiteDatabase openDataBase(){ SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase( DATABASES_DIR+DATABASE_NAME, null); return database; }
/** * * @param db * @return 查询全部的省 */ public List<ProvinceModel> getProvice(SQLiteDatabase db){ String sql="SELECT * FROM t_address_province ORDER BY id"; Cursor cursor = db.rawQuery(sql,null); List<ProvinceModel> list=new ArrayList<ProvinceModel>(); if (cursor!=null&&cursor.getCount() > 0) { while (cursor.moveToNext()){ ProvinceModel provinceModel=new ProvinceModel(); provinceModel.ID=cursor.getString(cursor.getColumnIndex("id")); provinceModel.NAME=cursor.getString(cursor.getColumnIndex("name")); provinceModel.CODE = cursor.getString(cursor.getColumnIndex("code")); list.add(provinceModel); } } return list; } /** * 依据省code查询全部的市 * @param db * @param code * @return */ public List<CityModel> getCityByParentId(SQLiteDatabase db,String code){ String sql="SELECT * FROM t_address_city WHERE provinceCode=? ORDER BY id"; Cursor cursor = db.rawQuery(sql,new String[]{code}); List<CityModel> list=new ArrayList<CityModel>(); if (cursor!=null&&cursor.getCount() > 0) { while (cursor.moveToNext()){ CityModel cityModel=new CityModel(); cityModel.ID=cursor.getString(cursor.getColumnIndex("id")); cityModel.NAME=cursor.getString(cursor.getColumnIndex("name")); cityModel.CODE = cursor.getString(cursor.getColumnIndex("code")); list.add(cityModel); } } return list; } /** * 依据市code查询全部的区 * @param db * @param code * @return */ public List<DistrictModel> getDistrictById(SQLiteDatabase db,String code){ String sql="SELECT * FROM t_address_town WHERE cityCode=?ORDER BY id "; Cursor cursor = db.rawQuery(sql,new String[]{code}); List<DistrictModel> list=new ArrayList<DistrictModel>(); if (cursor!=null&&cursor.getCount() > 0) { while (cursor.moveToNext()){ DistrictModel districtModel=new DistrictModel(); districtModel.ID=cursor.getString(cursor.getColumnIndex("id")); districtModel.NAME=cursor.getString(cursor.getColumnIndex("name")); districtModel.CODE = cursor.getString(cursor.getColumnIndex("code")); list.add(districtModel); } } return list; }
/** * Created by xuan on 16/1/7. */ public class ProvinceAdapter extends AbstractWheelTextAdapter { public List<ProvinceModel> mList; private Context mContext; public ProvinceAdapter(Context context,List<ProvinceModel> list) { super(context); mList=list; mContext=context; } @Override protected CharSequence getItemText(int index) { ProvinceModel provinceModel=mList.get(index); return provinceModel.NAME; } @Override public int getItemsCount() { return mList.size(); } }再将Adapter给WheelView就能够了
WheelView实现省市区三级联动(数据库实现版本号附带完整SQL及数据)
标签:== target blog where 文件大小 出错 amp .net class
原文地址:http://www.cnblogs.com/mthoutai/p/7202402.html