本文实例讲述了android autocompletetextview连接数据库自动提示的方法。分享给大家供大家参考,具体如下:
这个简单例子也体现mvc的思想。autocompletetextview 就是view,而simplecursoradapter就是controller,sqliteopenhelper就相当于model。
1、首先定义mvc中的model,自定义dbhelper类继承sqliteopenhelper用于访问数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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 下午4:16:02 */ public class dbhelper extends sqliteopenhelper { private static final int database_version = 1 ; private static final string database_name = "autocomplete.db" ; // 根据name自动查询 public static final string name = "name" ; public dbhelper(context context) { super (context, database_name, null , database_version); } @override public void oncreate(sqlitedatabase db) { string sqlstring = "createtable test (_id integer primary key autoincrement,name varchat(20) not null onconflict fail)" ; db.execsql(sqlstring); // 初始数据库表 string[] namestrarraystr = new string[] { "aaa" , "abc" , "cde" , "中国" , "美女" , "提示" }; 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) { // do nothing here } /** * 根据输入内容模糊查询 * @param name * @return */ public cursor query(string name) { sqlitedatabase db = this .getreadabledatabase(); return db.rawquery( "select* from test where name like '%" + name + "%' limit 10" , null ); } } |
2、定义autocompleteadater继承simplecursoradapter控制数据交互
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
import android.content.context; import android.database.cursor; import android.widget.simplecursoradapter; /** * * @author lai_zs * @date:2012-2-16 下午3:49:25 */ public class autocompleteadater extends simplecursoradapter { private dbhelper dbhelper = null ; private context context; // 查询字段 private string queryfield; public autocompleteadater(context context, int layout, cursor c,string from, int to) { super (context, layout, c, new string[] { from }, new int [] { to }); this .context = context; this .queryfield = from; } /** * 动态查询数据库 */ @override public cursor runqueryonbackgroundthread(charsequenceconstraint) { if (constraint != null ) { return getdbhelper().query((string) constraint); } else { return null ; } } /** * 这里设置在弹出的提示列表中点击某一项后的返回值,返回值将被显示在文本框中 */ @override public charsequence converttostring(cursor cursor) { return cursor.getstring(cursor.getcolumnindex(queryfield)); } public dbhelper getdbhelper() { if (dbhelper == null ) { dbhelper = new dbhelper( this .context); } return dbhelper; } } |
3、最后定义view
1
2
3
4
5
6
7
8
|
<autocompletetextview android:id= "@+id/autocompletetextview1" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "" android:hint= "@string/dbautocomlete" > <requestfocus /> </autocompletetextview> |
4、在activity中关联view和adapter
1
2
3
4
5
6
7
8
9
|
@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); // 设置输入一个字符就弹出提示列表(默认输入两个字符时才弹出提示) ((autocompletetextview) this .findviewbyid(r.id.autocompletetextview1)).setthreshold( 1 ); ((autocompletetextview) this .findviewbyid(r.id.autocompletetextview1)).setadapter(cursoradapter); } |
完整实例代码点击此处本站下载。
希望本文所述对大家android程序设计有所帮助。