Android SQLite with Autocompletetextview

Source: Internet
Author: User

Reading the data in SQLite is displayed in Autocompletetextview, which supports dynamically joining data that does not exist in SQLite.

Package Zhang.ya;import Java.io.file;import java.io.fileoutputstream;import java.io.inputstream;import Android.app.activity;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.os.bundle;import Android.text.editable;import Android.text.textwatcher;import Android.util.log;import Android.view.layoutinflater;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.AutoCompleteTextView ; import Android.widget.button;import android.widget.cursoradapter;import android.widget.TextView; Public classTest00 extends Activity implements Textwatcher, onclicklistener{PrivateFinal String Database_path =android.os.Environment.getExternalStorageDirectory (). GetAbsolutePath ()+"/course_name"; PrivateAutocompletetextview Coursename; PrivateFinal String Database_filename ="course_name.db3"; Privatesqlitedatabase database; PrivateButton Btnselectword; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); Database=OpenDatabase (); Coursename=(Autocompletetextview) Findviewbyid (r.id.coursename); Coursename.setthreshold (1); Coursename.addtextchangedlistener ( This); Btnselectword=(Button) Findviewbyid (r.id.buttonname); Btnselectword.setonclicklistener ( This); }     Public classCoursenameadapter extends CursorAdapter {PrivateLayoutinflater Layoutinflater; @Override Publiccharsequence converttostring (cursor cursor) {returncursor = =NULL?"": Cursor.getstring (Cursor.getcolumnindex ("Course_name")); }        Private voidSetview (view view, cursor cursor) {TextView Tvworditem=(TextView) view; Tvworditem.settext (cursor.getstring (Cursor.getcolumnindex ("Course_name"))); } @Override Public voidBindView (view view, context context, cursor cursor) {Setview (view, cursor); } @Override PublicView Newview (context context, cursor cursor, viewgroup parent) {View View= Layoutinflater.inflate (R.layout.word_list_item,NULL);            Setview (view, cursor); returnview; }         PublicCoursenameadapter (context context, Cursor C, Boolean autorequery) {Super (context, C, autorequery); Layoutinflater=(Layoutinflater) Context.getsystemservice (Context.layout_inflater_service); }    }    //NULL to make the OK button invalid, only input data to make the button active@Override Public voidaftertextchanged (Editable s) {log.i ("Zhangya","Textchage"); String Contentstr=s.tostring (); if(Contentstr = =NULL|| Contentstr.length () <=0)//Determines whether the contentstr is empty , and determines whether the string is a typical notation{log.i ("Zhangya","aftertextchanged NULL"); Btnselectword.setenabled (false);//NULL is not a can button        } Else{log.i ("Zhangya","aftertextchanged NOT NULL"); Btnselectword.setenabled (true); Cursor Cursor= Database.rawquery ("SELECT * from Course_name where course_name?",Newstring[] {contentstr+"%" }); Coursenameadapter Dictionaryadapter=NewCoursenameadapter ( This, cursor,true);        Coursename.setadapter (Dictionaryadapter); }} @Override Public voidBeforetextchanged (Charsequence S,intStartintCountintAfter ) {        //TODO auto-generated Method Stub} @Override Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) {    }    Privatesqlitedatabase OpenDatabase () {Try{String DatabaseFileName= Database_path +"/"+Database_filename; File dir=NewFile (Database_path); if(!dir.exists ()) Dir.mkdir (); if(! (NewFile (DatabaseFileName)). Exists ()) {InputStream is=getresources (). Openrawresource (R.raw.course_name); FileOutputStream Fos=NewFileOutputStream (DatabaseFileName); byte[] buffer =New byte[8192]; intCount =0;  while(Count = is. Read (buffer)) >0) {fos.write (buffer,0, Count);                } fos.close ();  is. Close (); } sqlitedatabase Database= Sqlitedatabase.openorcreatedatabase (DatabaseFileName,NULL); returndatabase; } Catch(Exception e) {}return NULL; } @Override Public voidOnClick (View v) {String SQL="SELECT * from Course_name where course_name=?"; Cursor Cursor= Database.rawquery (SQL,Newstring[] {coursename.gettext (). toString ()}); if(Cursor.getcount () = =0)//the data is inserted without a record of the same name{SQL="INSERT INTO Course_name (course_name) VALUES (?)"; Database.execsql (SQL,Newobject[] {coursename.gettext (). toString ()}); } Else{log.i ("Zhangya","Else");    } cursor.movetofirst (); }}

Android SQLite with Autocompletetextview

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.