Recently in the implementation of the delivery address function, using the provincial urban three-level linkage effect, found on the web is generally XML or JSON. Stale data source changes are troublesome. Changed the use of the database way to achieve a bit
Data source resolution. Because the amount of data is relatively large by initializing the bulk of the way SQL is not appropriate, time-consuming and easy error, I use the computer to create a good database and then put the database files in the program inside, by the following method can be copied to the DB files in the free folder. I put the files under the Raw folder in the demo.
/** * * @param instream * @param filenme file name * @param newpath directory path to be copied */public void copyFile (in Putstream instream,string Filenme, String NewPath) {try {int bytesum = 0; int byteread = 0; File File = new file (NewPath); Ensure that the directory exists if (!file.exists ()) {File.mkdir (); }//Assuming that the file exists with the Overwrite files 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;//bytes File size System.ou T.println (bytesum); Fs.write (buffer, 0, byteread); } instream.close (); Fs.close (); } CATCH (Exception e) {System.out.println ("Error copying file operation"); E.printstacktrace (); } }
With a database file, you just have to be able to do the database exercises by using the following method, which returns a reference to a given database.
/** * Open database file * @return * /Public sqlitedatabase OpenDatabase () { Sqlitedatabase db = Sqlitedatabase.openorcreatedatabase ( databases_dir+database_name, null); return database; }
The next step is querying the data source.
/** * * @param db * @return Query all Provinces */public list<provincemodel> Getprovice (Sqlitedatabase db) { String sql= "SELECT * from T_address_province the 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; /** * Query all cities by province code * @param DB * @param code * @return */public list<citymodel> getcit Ybyparentid (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 CIT Ymodel=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; /** * Query All zones * @param db * @param code * @return */public list<districtmodel> GE according to city code Tdistrictbyid (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; }
with the data source, we're done. Two-thirds, implement your own adapter by inheriting Abstractwheeltextadapter take the province for example
/** * 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 ();} }
and give the adapter to Wheelview.full version of demo and SQL attached
Wheelview implementation of the provincial three-level linkage (Database implementation version number with full SQL and data)