SQLite与AutoCompleteTextView结合使用

SQLite与AutoCompleteTextView结合使用,第1张

概述读取SQLite中的数据显示在AutoCompleteTextView中,支持动态加入SQLite中不存在的数据. package zhang.ya;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import android.app.Activity;import andr

读取sqlite中的数据显示在autoCompleteTextVIEw中,支持动态加入sqlite中不存在的数据.

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,c,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,cursor,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.输入后:

总结

以上是内存溢出为你收集整理的SQLite与AutoCompleteTextView结合使用全部内容,希望文章能够帮你解决SQLite与AutoCompleteTextView结合使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1182516.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存