Android AutoCompleteTextView连接数据库自动提示的方法(附demo源码下载)
本文实例讲述了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; /** * * @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控制数据交互
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
<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程序设计有所帮助。
赞 (0)