SQLite与AutoCompleteTextView结合使用

前端之家收集整理的这篇文章主要介绍了SQLite与AutoCompleteTextView结合使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
sqlite中的数据显示在AutoCompleteTextView中,支持动态加入sqlite中不存在的数据.
    @H_404_8@packagezhang.ya;
  1. @H_404_8@importjava.io.File;
  2. importjava.io.FileOutputStream;
  3. @H_404_8@importjava.io.InputStream;
  4. @H_404_8@importandroid.app.Activity;
  5. importandroid.content.Context;
  6. @H_404_8@importandroid.database.Cursor;
  7. importandroid.database.sqlite.sqliteDatabase;
  8. @H_404_8@importandroid.os.Bundle;
  9. importandroid.text.Editable;
  10. @H_404_8@importandroid.text.TextWatcher;
  11. importandroid.util.Log;
  12. @H_404_8@importandroid.view.LayoutInflater;
  13. importandroid.view.View;
  14. @H_404_8@importandroid.view.View.OnClickListener;
  15. importandroid.view.ViewGroup;
  16. @H_404_8@importandroid.widget.AutoCompleteTextView;
  17. importandroid.widget.Button;
  18. @H_404_8@importandroid.widget.CursorAdapter;
  19. importandroid.widget.TextView;
  20. @H_404_8@
  21. publicclassTest00extendsActivityimplementsTextWatcher,OnClickListener
  22. @H_404_8@{
  23. privatefinalStringDATABASE_PATH=android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
  24. @H_404_8@+"/course_name";
  25. privateAutoCompleteTextViewcourseName;
  26. @H_404_8@privatefinalStringDATABASE_FILENAME="course_name.db3";
  27. privatesqliteDatabasedatabase;
  28. @H_404_8@privateButtonbtnSelectWord;
  29. @H_404_8@@Override
  30. publicvoidonCreate(BundlesavedInstanceState)
  31. @H_404_8@{
  32. super.onCreate(savedInstanceState);
  33. @H_404_8@
  34. setContentView(R.layout.main);
  35. @H_404_8@database=openDatabase();
  36. courseName=(AutoCompleteTextView)findViewById(R.id.courseName);
  37. @H_404_8@courseName.setThreshold(1);
  38. courseName.addTextChangedListener(this);
  39. @H_404_8@btnSelectWord=(Button)findViewById(R.id.buttonName);
  40. btnSelectWord.setOnClickListener(this);
  41. @H_404_8@}
  42. @H_404_8@publicclassCourseNameAdapterextendsCursorAdapter
  43. {
  44. @H_404_8@privateLayoutInflaterlayoutInflater;
  45. @H_404_8@@Override
  46. publicCharSequenceconvertToString(Cursorcursor)
  47. @H_404_8@{
  48. returncursor==null?"":cursor.getString(cursor.getColumnIndex("course_name"));
  49. @H_404_8@}
  50. @H_404_8@privatevoidsetView(Viewview,Cursorcursor)
  51. {
  52. @H_404_8@TextViewtvWordItem=(TextView)view;
  53. tvWordItem.setText(cursor.getString(cursor.getColumnIndex("course_name")));
  54. @H_404_8@}
  55. @H_404_8@@Override
  56. publicvoidbindView(Viewview,Contextcontext,Cursorcursor)
  57. @H_404_8@{
  58. setView(view,cursor);
  59. @H_404_8@}
  60. @H_404_8@@Override
  61. publicViewnewView(Contextcontext,Cursorcursor,ViewGroupparent)
  62. @H_404_8@{
  63. Viewview=layoutInflater.inflate(R.layout.word_list_item,null);
  64. @H_404_8@setView(view,cursor);
  65. returnview;
  66. @H_404_8@}
  67. @H_404_8@publicCourseNameAdapter(Contextcontext,Cursorc,booleanautoRequery)
  68. {
  69. @H_404_8@super(context,c,autoRequery);
  70. layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  71. @H_404_8@}
  72. }
  73. @H_404_8@
  74. //输入为空则使得确定按钮失效,只有输入了数据才使得按钮处于活动状态
  75. @H_404_8@@Override
  76. publicvoidafterTextChanged(Editables)
  77. @H_404_8@{
  78. Log.i("zhangya","textchage");
  79. @H_404_8@StringcontentStr=s.toString();
  80. @H_404_8@if(contentStr==null||contentStr.length()<=0)//判断contentStr是否为空,判断字符串是否为空典型写法
  81. {
  82. @H_404_8@Log.i("zhangya","afterTextChangednull");
  83. btnSelectWord.setEnabled(false);//为空则不是能按钮
  84. @H_404_8@
  85. }else
  86. @H_404_8@{
  87. Log.i("zhangya","afterTextChangednotnull");
  88. @H_404_8@btnSelectWord.setEnabled(true);
  89. Cursorcursor=database.rawQuery("select*fromcourse_namewherecourse_namelike?",newString[]
  90. @H_404_8@{contentStr+"%"});
  91. @H_404_8@CourseNameAdapterdictionaryAdapter=newCourseNameAdapter(this,cursor,true);
  92. courseName.setAdapter(dictionaryAdapter);
  93. @H_404_8@}
  94. }
  95. @H_404_8@
  96. @Override
  97. @H_404_8@publicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter)
  98. {
  99. @H_404_8@//TODOAuto-generatedmethodstub
  100. @H_404_8@}
  101. @H_404_8@@Override
  102. publicvoidonTextChanged(CharSequences,intbefore,intcount)
  103. @H_404_8@{
  104. @H_404_8@}
  105. @H_404_8@privatesqliteDatabaSEOpenDatabase()
  106. {
  107. @H_404_8@try
  108. {
  109. @H_404_8@StringdatabaseFilename=DATABASE_PATH+"/"+DATABASE_FILENAME;
  110. Filedir=newFile(DATABASE_PATH);
  111. @H_404_8@if(!dir.exists())
  112. dir.mkdir();
  113. @H_404_8@if(!(newFile(databaseFilename)).exists())
  114. {
  115. @H_404_8@InputStreamis=getResources().openRawResource(R.raw.course_name);
  116. FileOutputStreamfos=newFileOutputStream(databaseFilename);
  117. @H_404_8@byte[]buffer=newbyte[8192];
  118. intcount=0;
  119. @H_404_8@while((count=is.read(buffer))>0)
  120. {
  121. @H_404_8@fos.write(buffer,0,count);
  122. }
  123. @H_404_8@
  124. fos.close();
  125. @H_404_8@is.close();
  126. }
  127. @H_404_8@sqliteDatabasedatabase=sqliteDatabase.openOrCreateDatabase(databaseFilename,null);
  128. returndatabase;
  129. @H_404_8@}catch(Exceptione)
  130. {
  131. @H_404_8@}
  132. returnnull;
  133. @H_404_8@}
  134. @H_404_8@@Override
  135. publicvoidonClick(Viewv)
  136. @H_404_8@{
  137. Stringsql="select*fromcourse_namewherecourse_name=?";
  138. @H_404_8@Cursorcursor=database.rawQuery(sql,newString[]
  139. {courseName.getText().toString()});
  140. @H_404_8@if(cursor.getCount()==0)//没有同名记录,则插入数据
  141. {
  142. @H_404_8@sql="insertintocourse_name(course_name)values(?)";
  143. database.execsql(sql,newObject[]
  144. @H_404_8@{courseName.getText().toString()});
  145. }else
  146. @H_404_8@{
  147. Log.i("zhangya","else");
  148. @H_404_8@}
  149. cursor.moveToFirst();
  150. @H_404_8@
  151. }
  152. @H_404_8@
  153. }

1.未输入时状态:

2.输入后:

猜你在找的Sqlite相关文章