一、android开发中AutoCompleteTextView的使用方法详解
1、创建适配器类DictionaryAdapter使它扩展自CursorAdapter,在类DictionaryAdapter中需要建立一个构造函数DictionaryAdapter(Context context, Cursor c, boolean autoRequery),同时需要重写convertToString(Cursor cursor),setView(View view , Cursor cursor),bindView(View arg0, Context arg1, Cursor arg2)以及newView(Context arg0, Cursor arg1, ViewGroup arg2)方法
- import android.content.Context;
- import android.database.Cursor;
- import android.support.v4.widget.CursorAdapter;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
-
- public class DictionaryAdapter extends CursorAdapter {
-
- private Cursor cursor;
-
- private TextView tvWordItem;
-
- private View view;
-
- private LayoutInflater layoutInflater;
-
-
- public DictionaryAdapter(Context context, Cursor c, boolean autoRequery) {
- super(context, c, autoRequery);
- layoutInflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
- }
-
- public CharSequence convertToString(Cursor cursor){
- return cursor==null ? "" : cursor.getString(cursor.getColumnIndex("_id"));
- }
-
- private void setView(View view , Cursor cursor) {
-
-
- tvWordItem=(TextView) view;
- tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_id")));
-
- }
-
- @Override
- public void bindView(View arg0, Context arg1, Cursor arg2) {
-
-
- setView(arg0, arg2);
-
- }
-
- @Override
- public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
-
-
- view=layoutInflater.inflate(R.layout.word_list_item, null);
- setView(view, arg1);
-
- return view;
- }
-
- }
2、重写afterTextChanged(Editable s)方法
- public void afterTextChanged(Editable s) {
-
- }
3、从数据库中查询相关信息
- private Cursor cursor;
- private SQLiteDatabase database;
- database=new Login().openDatabase();
- //openDatabase为Login类中的一个方法,用来将文件夹中的数据库复制到手机中的相应位置
- public SQLiteDatabase openDatabase()
- {
- try
- {
- // 获得dictionary.db文件的绝对路径
- String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
- File dir = new File(DATABASE_PATH);
- // 如果/sdcard/dictionary目录中不存在,创建这个目录
- if (!dir.exists())
- dir.mkdir();
- // 如果在/sdcard/dictionary目录中不存在
- // dictionary.db文件,则从res\raw目录中复制这个文件到
- // SD卡的目录(/sdcard/dictionary)
- if (!(new File(databaseFilename)).exists())
- {
- // 获得封装dictionary.db文件的InputStream对象
- InputStream is = getResources().openRawResource(
- R.raw.dictionary);
- FileOutputStream fos = new FileOutputStream(databaseFilename);
- byte[] buffer = new byte[8192];
- int count = 0;
- // 开始复制dictionary.db文件
- while ((count = is.read(buffer)) > 0)
- {
- fos.write(buffer, 0, count);
- }
- //关闭文件流
- fos.close();
- is.close();
- }
- // 打开/sdcard/dictionary目录中的dictionary.db文件
- SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
- databaseFilename, null);
- return database;
- }
- catch (Exception e)
- {
- }
- //如果打开出错,则返回null
- return null;
- }
- select_sql_1="select english as _id from t_words where english like ?";
- cursor=database.rawQuery(select_sql_1, new String[]{s.toString()+"%"});
4、为AutoCompleteTextView设置适配器
- private DictionaryAdapter dictionaryAdapter;
- dictionaryAdapter=new DictionaryAdapter(this, cursor, true);
- actInputWords.setAdapter(dictionaryAdapter);
二、示例
三、源代码
本文中完整源代码下载地址
CSDN:http://download.csdn.net/detail/wpwbb510582246/9644468
Github:https://github.com/wpwbb510582246/MyDitionary
由于本人初写博客,写的不好的地方还请大家能批评指正,希望能和大家相互学习、相互交流、共同成长。