Wheelview implementation of the provincial three-level linkage (Database implementation version number with full SQL and data)

Source: Internet
Author: User

Recently in the implementation of the delivery address function. The use of the provincial urban three-level linkage effect. Found on the Internet is generally XML or JSON, data source stale change trouble, changed the use of the database to achieve a bit

Data source resolution, because the amount of data is relatively large by initializing the bulk of the way to run SQL inappropriate, time-consuming and easy error, I use the computer to create a good database and then put the database files in the program inside, through the following method to copy the db file into a random 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.