SQLite与AutoCompleteTextView结合使用

来源:互联网 发布:debug.print如何使用vb 编辑:程序博客网 时间:2024/05/18 22:44
取SQLite中的数据显示在AutoCompleteTextView中,支持动态加入SQLite中不存在的数据.

[java] view plaincopyprint?
  1. package zhang.ya;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStream;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Context;  
  9. import android.database.Cursor;  
  10. import android.database.sqlite.SQLiteDatabase;  
  11. import android.os.Bundle;  
  12. import android.text.Editable;  
  13. import android.text.TextWatcher;  
  14. import android.util.Log;  
  15. import android.view.LayoutInflater;  
  16. import android.view.View;  
  17. import android.view.View.OnClickListener;  
  18. import android.view.ViewGroup;  
  19. import android.widget.AutoCompleteTextView;  
  20. import android.widget.Button;  
  21. import android.widget.CursorAdapter;  
  22. import android.widget.TextView;  
  23.   
  24. public class Test00 extends Activity implements TextWatcher, OnClickListener  
  25. {  
  26.     private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()  
  27.             + "/course_name";  
  28.     private AutoCompleteTextView courseName;  
  29.     private final String DATABASE_FILENAME = "course_name.db3";  
  30.     private SQLiteDatabase database;  
  31.     private Button btnSelectWord;  
  32.   
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState)  
  35.     {  
  36.         super.onCreate(savedInstanceState);  
  37.   
  38.         setContentView(R.layout.main);  
  39.         database = openDatabase();  
  40.         courseName = (AutoCompleteTextView) findViewById(R.id.courseName);  
  41.         courseName.setThreshold(1);  
  42.         courseName.addTextChangedListener(this);  
  43.         btnSelectWord = (Button) findViewById(R.id.buttonName);  
  44.         btnSelectWord.setOnClickListener(this);  
  45.     }  
  46.   
  47.     public class CourseNameAdapter extends CursorAdapter  
  48.     {  
  49.         private LayoutInflater layoutInflater;  
  50.   
  51.         @Override  
  52.         public CharSequence convertToString(Cursor cursor)  
  53.         {  
  54.             return cursor == null ? "" : cursor.getString(cursor.getColumnIndex("course_name"));  
  55.         }  
  56.   
  57.         private void setView(View view, Cursor cursor)  
  58.         {  
  59.             TextView tvWordItem = (TextView) view;  
  60.             tvWordItem.setText(cursor.getString(cursor.getColumnIndex("course_name")));  
  61.         }  
  62.   
  63.         @Override  
  64.         public void bindView(View view, Context context, Cursor cursor)  
  65.         {  
  66.             setView(view, cursor);  
  67.         }  
  68.   
  69.         @Override  
  70.         public View newView(Context context, Cursor cursor, ViewGroup parent)  
  71.         {  
  72.             View view = layoutInflater.inflate(R.layout.word_list_item, null);  
  73.             setView(view, cursor);  
  74.             return view;  
  75.         }  
  76.   
  77.         public CourseNameAdapter(Context context, Cursor c, boolean autoRequery)  
  78.         {  
  79.             super(context, c, autoRequery);  
  80.             layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  81.         }  
  82.     }  
  83.   
  84.     //输入为空则使得确定按钮失效,只有输入了数据才使得按钮处于活动状态  
  85.     @Override  
  86.     public void afterTextChanged(Editable s)  
  87.     {  
  88.         Log.i("zhangya""textchage");  
  89.         String contentStr = s.toString();  
  90.           
  91.         if (contentStr == null || contentStr.length() <= 0)//判断contentStr是否为空,判断字符串是否为空典型写法  
  92.         {  
  93.             Log.i("zhangya""afterTextChanged null");  
  94.             btnSelectWord.setEnabled(false);//为空则不是能按钮  
  95.   
  96.         } else  
  97.         {  
  98.             Log.i("zhangya""afterTextChanged not null");  
  99.             btnSelectWord.setEnabled(true);  
  100.             Cursor cursor = database.rawQuery("select * from course_name where course_name like ?"new String[]  
  101.             { contentStr + "%" });  
  102.   
  103.             CourseNameAdapter dictionaryAdapter = new CourseNameAdapter(this, cursor, true);  
  104.             courseName.setAdapter(dictionaryAdapter);  
  105.         }  
  106.     }  
  107.   
  108.     @Override  
  109.     public void beforeTextChanged(CharSequence s, int start, int count, int after)  
  110.     {  
  111.         // TODO Auto-generated method stub  
  112.   
  113.     }  
  114.   
  115.     @Override  
  116.     public void onTextChanged(CharSequence s, int start, int before, int count)  
  117.     {  
  118.   
  119.     }  
  120.   
  121.     private SQLiteDatabase openDatabase()  
  122.     {  
  123.         try  
  124.         {  
  125.             String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;  
  126.             File dir = new File(DATABASE_PATH);  
  127.             if (!dir.exists())  
  128.                 dir.mkdir();  
  129.             if (!(new File(databaseFilename)).exists())  
  130.             {  
  131.                 InputStream is = getResources().openRawResource(R.raw.course_name);  
  132.                 FileOutputStream fos = new FileOutputStream(databaseFilename);  
  133.                 byte[] buffer = new byte[8192];  
  134.                 int count = 0;  
  135.                 while ((count = is.read(buffer)) > 0)  
  136.                 {  
  137.                     fos.write(buffer, 0, count);  
  138.                 }  
  139.   
  140.                 fos.close();  
  141.                 is.close();  
  142.             }  
  143.             SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);  
  144.             return database;  
  145.         } catch (Exception e)  
  146.         {  
  147.         }  
  148.         return null;  
  149.     }  
  150.   
  151.     @Override  
  152.     public void onClick(View v)  
  153.     {  
  154.         String sql = "select * from course_name where course_name=?";  
  155.         Cursor cursor = database.rawQuery(sql, new String[]  
  156.         { courseName.getText().toString() });  
  157.         if (cursor.getCount() == 0)//没有同名记录,则插入数据  
  158.         {  
  159.             sql = "insert into course_name(course_name)values(?)";  
  160.             database.execSQL(sql, new Object[]  
  161.             { courseName.getText().toString() });  
  162.         } else  
  163.         {  
  164.             Log.i("zhangya""else");  
  165.         }  
  166.         cursor.moveToFirst();  
  167.   
  168.     }  
  169.   
  170. }  

1.未输入时状态:       

2.输入后: