本文实例讲述了Android AutoCompleteTextView连接数据库自动提示的方法。分享给大家供大家参考,具体如下:
这个简单例子也体现MVC的思想。AutoCompleteTextView 就是View,而SimpleCursorAdapter就是Controller,SQLiteOpenHelper就相当于Model。
1、首先定义MVC中的Model,自定义DBHelper类继承SQLiteOpenHelper用于访问数据库
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
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
}
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控制数据交互
import android.content.Context;
import android.database.Cursor;
import android.widget.SimpleCursorAdapter;
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
<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
@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相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:Android高级组件AutoCompleteTextView自动完成文本框使用详解Android中EditText和AutoCompleteTextView设置文字选中颜色方法Android AutoCompleteTextView控件使用实例基于Android中的 AutoCompleteTextView实现自动填充实例讲解Android中的AutoCompleteTextView自动补全组件Android仿百度谷歌搜索自动提示框AutoCompleteTextView简单应用示例Android自动编辑文本框(AutoCompleteTextView)使用方法详解Android中AutoCompleteTextView自动提示android中AutoCompleteTextView的简单用法(实现搜索历史)Android AutoCompleteTextView控件基本用法示例Android开发高级组件之自动完成文本框(AutoCompleteTextView)用法示例【附源码下载】