android开发中AutoCompleteTextView的使用方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wpwbb510582246/article/details/52717921

一、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)方法

  1. import android.content.Context;
  2. import android.database.Cursor;
  3. import android.support.v4.widget.CursorAdapter;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8.  
  9. public class DictionaryAdapter extends CursorAdapter {
  10.  
  11. private Cursor cursor;
  12.  
  13. private TextView tvWordItem;
  14.  
  15. private View view;
  16.  
  17. private LayoutInflater layoutInflater;
  18.  
  19.  
  20. public DictionaryAdapter(Context context, Cursor c, boolean autoRequery) {
  21. super(context, c, autoRequery);
  22. layoutInflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
  23. }
  24.  
  25. public CharSequence convertToString(Cursor cursor){
  26. return cursor==null ? "" : cursor.getString(cursor.getColumnIndex("_id"));
  27. }
  28.  
  29. private void setView(View view , Cursor cursor) {
  30.  
  31.  
  32. tvWordItem=(TextView) view;
  33. tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_id")));
  34.  
  35. }
  36.  
  37. @Override
  38. public void bindView(View arg0, Context arg1, Cursor arg2) {
  39.  
  40.  
  41. setView(arg0, arg2);
  42.  
  43. }
  44.  
  45. @Override
  46. public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
  47.  
  48.  
  49. view=layoutInflater.inflate(R.layout.word_list_item, null);
  50. setView(view, arg1);
  51.  
  52. return view;
  53. }
  54.  
  55. }

2、重写afterTextChanged(Editable s)方法

  1. public void afterTextChanged(Editable s) {
  2. }

3、从数据库中查询相关信息

  1. private Cursor cursor;
  1. private SQLiteDatabase database;
  1. database=new Login().openDatabase();
  2. //openDatabase为Login类中的一个方法,用来将文件夹中的数据库复制到手机中的相应位置
  1. public SQLiteDatabase openDatabase()
  2. {
  3. try
  4. {
  5. // 获得dictionary.db文件的绝对路径
  6. String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
  7. File dir = new File(DATABASE_PATH);
  8. // 如果/sdcard/dictionary目录中不存在,创建这个目录
  9. if (!dir.exists())
  10. dir.mkdir();
  11. // 如果在/sdcard/dictionary目录中不存在
  12. // dictionary.db文件,则从res\raw目录中复制这个文件到
  13. // SD卡的目录(/sdcard/dictionary)
  14. if (!(new File(databaseFilename)).exists())
  15. {
  16. // 获得封装dictionary.db文件的InputStream对象
  17. InputStream is = getResources().openRawResource(
  18. R.raw.dictionary);
  19. FileOutputStream fos = new FileOutputStream(databaseFilename);
  20. byte[] buffer = new byte[8192];
  21. int count = 0;
  22. // 开始复制dictionary.db文件
  23. while ((count = is.read(buffer)) > 0)
  24. {
  25. fos.write(buffer, 0, count);
  26. }
  27. //关闭文件流
  28. fos.close();
  29. is.close();
  30. }
  31. // 打开/sdcard/dictionary目录中的dictionary.db文件
  32. SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
  33. databaseFilename, null);
  34. return database;
  35. }
  36. catch (Exception e)
  37. {
  38. }
  39. //如果打开出错,则返回null
  40. return null;
  41. }
  1. select_sql_1="select english as _id from t_words where english like ?";
  2. cursor=database.rawQuery(select_sql_1, new String[]{s.toString()+"%"});

4、为AutoCompleteTextView设置适配器

  1. private DictionaryAdapter dictionaryAdapter;
 
 
  1. dictionaryAdapter=new DictionaryAdapter(this, cursor, true);
  2. actInputWords.setAdapter(dictionaryAdapter);

 二、示例

三、源代码

 

本文中完整源代码下载地址

 CSDN:http://download.csdn.net/detail/wpwbb510582246/9644468

Github:https://github.com/wpwbb510582246/MyDitionary

 

由于本人初写博客,写的不好的地方还请大家能批评指正,希望能和大家相互学习、相互交流、共同成长。

猜你喜欢

转载自blog.csdn.net/wpwbb510582246/article/details/52717921