The example in this article describes how the Android Autocompletetextview connection database automatic prompts. Share to everyone for your reference, specific as follows:
This simple example also embodies the idea of MVC. Autocompletetextview is view, and Simplecursoradapter is Controller,sqliteopenhelper is equivalent to model.
1, first define the model in MVC, custom DBHelper class inheritance Sqliteopenhelper used to access the database
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper; /** * * @author Lai_zs * @date: 2012-2-16 PM 4:16:02 * * public class DBHelper extends Sqliteopenhelper {private Stati
c final int database_version = 1;
private static final String database_name = "AUTOCOMPLETE.DB";
Automatically query public static final String name = "Name" according to name;
Public DBHelper {Super (context, database_name, NULL, database_version); @Override public void OnCreate (Sqlitedatabase db) {String SqlString = ' createtable Test (_id integer primary ke
Y autoincrement,name varchat () NOT null onconflict fail) ";
Db.execsql (SqlString);
Initial database table string[] namestrarraystr = new string[] {"AAA", "abc", "CDE", "China", "Beauty", "hint"}; for (int i = 0; i < namestrarraystr.length i++) {db.execsql ("INSERT intotest (" + NAME + ") VALUES (?)", New Object [] {namestrarraystr[i] }); } @Override public void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {//Does nothing here}/** * Based on the input fuzzy query * @param name * @return/Public Cursor query (String name) {Sqlitedatabase db = This.getreada
Bledatabase ();
Return Db.rawquery ("select* from test where name like '%" + name + "% ' limit", null);
}
}
2, defining Autocompleteadater inheritance Simplecursoradapter controlling data interaction
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.widget.SimpleCursorAdapter;
/** * * @author Lai_zs * @date: 2012-2-16 PM 3:49:25/public class Autocompleteadater extends Simplecursoradapter {
Private DBHelper dbhelper = null;
private context;
Query field private String Queryfield; Public Autocompleteadater (context, int layout, Cursor c,string from, int to) {Super (context, layout, C, new S
Tring[] {from},new int[] ({to});
This.context = context;
This.queryfield = from; /** * Dynamic Query Database */@Override public Cursor runqueryonbackgroundthread (charsequenceconstraint) {if (constr
Aint!= null) {return getdbhelper (). Query ((String) constraint);
else {return null; }/** * Here set the return value after clicking on an item in the pop-up list of prompts, the return value will be displayed in the text box * * @Override public charsequence converttostring (Cursor curso
R) {return cursor.getstring (Cursor.getcolumnindex (Queryfield)); } public DBHelper GetdbhelpeR () {if (DBHelper = null) {DBHelper = new DBHelper (this.context);
return dbhelper;
}
}
3. Final definition View
<autocompletetextview
android:id= "@+id/autocompletetextview1"
android:layout_width= "Match_parent"
android:layout_height= "wrap_content"
android:text= "
android:hint=" @string/dbautocomlete ">
<requestfocus/>
</AutoCompleteTextView>
4. Associating view and adapter in activity
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Autocompleteadatercursoradapter = new Autocompleteadater (this, Android. R.layout.simple_dropdown_item_1line,null, Dbhelper.name, Android. R.ID.TEXT1);
Set enter a character to pop-up the list of prompts (the default input two words Fu Shicai pop-up prompts)
((Autocompletetextview) This.findviewbyid (R.id.autocompletetextview1)). Setthreshold (1);
((Autocompletetextview) This.findviewbyid (R.id.autocompletetextview1)). Setadapter (CursorAdapter);
Full instance code click here to download the site.
More interested readers of Android-related content can view this site: Android Development Primer and Advanced tutorial, Android resource Operation tips Summary, Android View view tips Summary and Android control usage summary
I hope this article will help you with the Android program.