- packagezhang.ya;
- importjava.io.File;
- importjava.io.FileOutputStream;
- importjava.io.InputStream;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.database.Cursor;
- importandroid.database.sqlite.sqliteDatabase;
- importandroid.os.Bundle;
- importandroid.text.Editable;
- importandroid.text.TextWatcher;
- importandroid.util.Log;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.view.ViewGroup;
- importandroid.widget.AutoCompleteTextView;
- importandroid.widget.Button;
- importandroid.widget.CursorAdapter;
- importandroid.widget.TextView;
- publicclassTest00extendsActivityimplementsTextWatcher,OnClickListener
- {
- privatefinalStringDATABASE_PATH=android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
- +"/course_name";
- privateAutoCompleteTextViewcourseName;
- privatefinalStringDATABASE_FILENAME="course_name.db3";
- privatesqliteDatabasedatabase;
- privateButtonbtnSelectWord;
- @Override
- publicvoidonCreate(BundlesavedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- database=openDatabase();
- courseName=(AutoCompleteTextView)findViewById(R.id.courseName);
- courseName.setThreshold(1);
- courseName.addTextChangedListener(this);
- btnSelectWord=(Button)findViewById(R.id.buttonName);
- btnSelectWord.setOnClickListener(this);
- }
- publicclassCourseNameAdapterextendsCursorAdapter
- {
- privateLayoutInflaterlayoutInflater;
- @Override
- publicCharSequenceconvertToString(Cursorcursor)
- {
- returncursor==null?"":cursor.getString(cursor.getColumnIndex("course_name"));
- }
- privatevoidsetView(Viewview,Cursorcursor)
- {
- TextViewtvWordItem=(TextView)view;
- tvWordItem.setText(cursor.getString(cursor.getColumnIndex("course_name")));
- }
- @Override
- publicvoidbindView(Viewview,Contextcontext,Cursorcursor)
- {
- setView(view,cursor);
- }
- @Override
- publicViewnewView(Contextcontext,Cursorcursor,ViewGroupparent)
- {
- Viewview=layoutInflater.inflate(R.layout.word_list_item,null);
- setView(view,cursor);
- returnview;
- }
- publicCourseNameAdapter(Contextcontext,Cursorc,booleanautoRequery)
- {
- super(context,c,autoRequery);
- layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- }
- //输入为空则使得确定按钮失效,只有输入了数据才使得按钮处于活动状态
- @Override
- publicvoidafterTextChanged(Editables)
- {
- Log.i("zhangya","textchage");
- StringcontentStr=s.toString();
- if(contentStr==null||contentStr.length()<=0)//判断contentStr是否为空,判断字符串是否为空典型写法
- {
- Log.i("zhangya","afterTextChangednull");
- btnSelectWord.setEnabled(false);//为空则不是能按钮
- }else
- {
- Log.i("zhangya","afterTextChangednotnull");
- btnSelectWord.setEnabled(true);
- Cursorcursor=database.rawQuery("select*fromcourse_namewherecourse_namelike?",newString[]
- {contentStr+"%"});
- CourseNameAdapterdictionaryAdapter=newCourseNameAdapter(this,cursor,true);
- courseName.setAdapter(dictionaryAdapter);
- }
- }
- @Override
- publicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter)
- {
- //TODOAuto-generatedmethodstub
- }
- @Override
- publicvoidonTextChanged(CharSequences,intbefore,intcount)
- {
- }
- privatesqliteDatabaSEOpenDatabase()
- {
- try
- {
- StringdatabaseFilename=DATABASE_PATH+"/"+DATABASE_FILENAME;
- Filedir=newFile(DATABASE_PATH);
- if(!dir.exists())
- dir.mkdir();
- if(!(newFile(databaseFilename)).exists())
- {
- InputStreamis=getResources().openRawResource(R.raw.course_name);
- FileOutputStreamfos=newFileOutputStream(databaseFilename);
- byte[]buffer=newbyte[8192];
- intcount=0;
- while((count=is.read(buffer))>0)
- {
- fos.write(buffer,0,count);
- }
- fos.close();
- is.close();
- }
- sqliteDatabasedatabase=sqliteDatabase.openOrCreateDatabase(databaseFilename,null);
- returndatabase;
- }catch(Exceptione)
- {
- }
- returnnull;
- }
- @Override
- publicvoidonClick(Viewv)
- {
- Stringsql="select*fromcourse_namewherecourse_name=?";
- Cursorcursor=database.rawQuery(sql,newString[]
- {courseName.getText().toString()});
- if(cursor.getCount()==0)//没有同名记录,则插入数据
- {
- sql="insertintocourse_name(course_name)values(?)";
- database.execsql(sql,newObject[]
- {courseName.getText().toString()});
- }else
- {
- Log.i("zhangya","else");
- }
- cursor.moveToFirst();
- }
- }
package zhang.ya; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.CursorAdapter; import android.widget.TextView; public class Test00 extends Activity implements TextWatcher,OnClickListener { private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/course_name"; private AutoCompleteTextView courseName; private final String DATABASE_FILENAME = "course_name.db3"; private sqliteDatabase database; private Button btnSelectWord; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); database = openDatabase(); courseName = (AutoCompleteTextView) findViewById(R.id.courseName); courseName.setThreshold(1); courseName.addTextChangedListener(this); btnSelectWord = (Button) findViewById(R.id.buttonName); btnSelectWord.setOnClickListener(this); } public class CourseNameAdapter extends CursorAdapter { private LayoutInflater layoutInflater; @Override public CharSequence convertToString(Cursor cursor) { return cursor == null ? "" : cursor.getString(cursor.getColumnIndex("course_name")); } private void setView(View view,Cursor cursor) { TextView tvWordItem = (TextView) view; tvWordItem.setText(cursor.getString(cursor.getColumnIndex("course_name"))); } @Override public void bindView(View view,Context context,Cursor cursor) { setView(view,cursor); } @Override public View newView(Context context,Cursor cursor,ViewGroup parent) { View view = layoutInflater.inflate(R.layout.word_list_item,null); setView(view,cursor); return view; } public CourseNameAdapter(Context context,Cursor c,boolean autoRequery) { super(context,autoRequery); layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } } //输入为空则使得确定按钮失效,只有输入了数据才使得按钮处于活动状态 @Override public void afterTextChanged(Editable s) { Log.i("zhangya","textchage"); String contentStr = s.toString(); if (contentStr == null || contentStr.length() <= 0)//判断contentStr是否为空,判断字符串是否为空典型写法 { Log.i("zhangya","afterTextChanged null"); btnSelectWord.setEnabled(false);//为空则不是能按钮 } else { Log.i("zhangya","afterTextChanged not null"); btnSelectWord.setEnabled(true); Cursor cursor = database.rawQuery("select * from course_name where course_name like ?",new String[] { contentStr + "%" }); CourseNameAdapter dictionaryAdapter = new CourseNameAdapter(this,true); courseName.setAdapter(dictionaryAdapter); } } @Override public void beforeTextChanged(CharSequence s,int start,int count,int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s,int before,int count) { } private sqliteDatabase openDatabase() { try { String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; File dir = new File(DATABASE_PATH); if (!dir.exists()) dir.mkdir(); if (!(new File(databaseFilename)).exists()) { InputStream is = getResources().openRawResource(R.raw.course_name); FileOutputStream fos = new FileOutputStream(databaseFilename); byte[] buffer = new byte[8192]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer,count); } fos.close(); is.close(); } sqliteDatabase database = sqliteDatabase.openOrCreateDatabase(databaseFilename,null); return database; } catch (Exception e) { } return null; } @Override public void onClick(View v) { String sql = "select * from course_name where course_name=?"; Cursor cursor = database.rawQuery(sql,new String[] { courseName.getText().toString() }); if (cursor.getCount() == 0)//没有同名记录,则插入数据 { sql = "insert into course_name(course_name)values(?)"; database.execsql(sql,new Object[] { courseName.getText().toString() }); } else { Log.i("zhangya","else"); } cursor.moveToFirst(); } }
1.未输入时状态:
2.输入后:
原文链接:https://www.f2er.com/sqlite/201694.html