Learn Android has been about a week time, always feel that their foundation is not good, can only through 1.1 points accumulated knocking to consolidate the ordinary knowledge, and sometimes the first does not encapsulate the knock again, and then encapsulation, some statements really do not remember, although know what he means, then repeatedly knocked, refueling it! Junior, let's get down to the chase.
The Dbconn.java class mainly writes Raw to the DATA/DATA/XXX package/Bottom in Ddms, the code is as follows:
Package Com.tp.soft.util;import Java.io.file;import java.io.fileoutputstream;import java.io.inputstream;import Com.example.dbcity.r;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import android.os.Environment; Public classDbconn {Private StaticString db_name ="test.db"; Private StaticString package ="com.example.dbcity"; Private StaticString PATH ="/data"+environment.getdatadirectory (). GetPath ()+File.separator+Package ; Privatecontext Context; Privatesqlitedatabase database; PublicDbconn (Context context) { This. Context =context; } Public StaticFinal Dbconn getinstance (context context) {Dbconn conn=Newdbconn (context); returnConn; } Publicsqlitedatabase Getdb () {database= Opendb (PATH + file.separator +db_name); returndatabase; } Publicsqlitedatabase opendb (String dbfile) {Try{ if(!Isexitfile (DBFile)) {InputStream is=context.getresources (). Openrawresource (R.raw.test); FileOutputStream Fos=NewFileOutputStream (DBFile); byte[] buffer =New byte[1024x768]; intLen =-1; while(len = is. Read (buffer))! =-1) {fos.write (buffer,0, Len); } is. Close (); Fos.close (); } database= Sqlitedatabase.openorcreatedatabase (DBFile,NULL); returndatabase; }Catch(Exception e) {e.printstacktrace (); return NULL; } } Publicboolean isexitfile (String path) {File file=NewFile (path); returnfile.exists (); }}
Dbmanage.java is mainly on the database package, currently only do a query, to be completed later, the code is as follows:
Package Com.tp.soft.util;import java.util.arraylist;import java.util.list;import android.content.Context; Import Android.database.cursor;import android.database.sqlite.SQLiteDatabase; Public classDbmanage {PrivateSqlitedatabase DB; PublicDbmanage (Context context) {Dbconn conn=dbconn.getinstance (context); DB=Conn.getdb (); } PublicList<string>querydata (String sql, string[] Selectionargs, string property) {cursor cursor=NULL; List<String> list =NewArraylist<string>(); Try{cursor=db.rawquery (SQL, Selectionargs); while(Cursor.movetonext ()) {String str=cursor.getstring (Cursor.getcolumnindex (property)); List.add (str); } }Catch(Exception e) {e.printstacktrace (); }finally{ if(NULL!=cursor) {Cursor.close (); } } returnlist; } }
The main entrance is the display of a drop-down list, and then the SQLite database is called, some of the code is as follows:
ProSelect = (Spinner) Findviewbyid (r.id.proselect); New Dbmanage (this); List<String> prolist = Dbmanage.querydata ("select * from Au_user"null " username " ); ArrayadapterNew arrayadapter<string> (this, Android. R.layout.simple_gallery_item, prolist); Proselect.setadapter (arrayadapter);
Summed up a sentence, in fact, the code to knock more than to think how to knock, and not a little did not knock to think how to hit the best sentence I think this is impossible, rookie self-study distance ...