SQLite与AutoCompleteTextView结合使用

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

1.未输入时状态:

2.输入后:

猜你在找的Sqlite相关文章