Code:
Mainactivity.java
Package com.example.happydictionary;
Import Com.example.happydictionary.adapter.DictionaryAdapter;
Import Com.example.happydictionary.db.DBHelper;
Import android.app.Activity;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.os.Bundle;
Import android.text.Editable;
Import Android.text.TextWatcher;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.AutoCompleteTextView;
Import Android.widget.Button;
Import Android.widget.TextView;
public class Mainactivity extends Activity implements Onclicklistener,
Textwatcher {
Private DBHelper DBHelper; User input text box
Private Autocompletetextview word; Define the name of the database
Private Sqlitedatabase database;
Private Button Searchword; Search button
Private TextView Showresult; User Displays query results
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
DBHelper = new DBHelper (Getbasecontext ());//Open database
Database = Dbhelper.opendatabase ();
Init ();
Searchword.setonclicklistener (this); Binding listeners
Word.addtextchangedlistener (this); Binding text Change Listener
}
public void init () {
Searchword = (Button) Findviewbyid (R.id.btnsearch);
Word = (Autocompletetextview) Findviewbyid (R.id.etword);
Showresult = (TextView) Findviewbyid (R.id.tvsearchresult);
}
public void aftertextchanged (Editable s) {
cursor cursor = Database.rawquery (
"Select 中文版 as _id from T_words where 中文版 like?",
New string[] {s.tostring () + "%"});
Create a new adapter
Dictionaryadapter dictionaryadapter = new Dictionaryadapter (This,cursor, true);
Binding Adapter
Word.setadapter (Dictionaryadapter);
}
public void beforetextchanged (charsequence s, int start, int count,
int after) {
}
public void ontextchanged (charsequence s, int start, int before, int count) {
}
public void OnClick (view view) {
Query the specified word
String sql = "Select Chinese from t_words where english=?";
cursor cursor = database.rawquery (sql, new string[] {word.gettext ()
. toString ()});
String result = "No such word found"; If you look up a word, display its meaning in Chinese
if (Cursor.getcount () > 0) {
Cursor.movetofirst (); You must use the Movetofirst method to move the record pointer to the position of the 1th record
result = Cursor.getstring (Cursor.getcolumnindex ("Chinese"))
. Replace ("&", "&");
}
Showresult.settext (Word.gettext () + "\ n" + result.tostring ());//show the results to TextView
}
}
Dictionaryadapter.java
</pre><pre name= "code" class= "Java" >package com.example.happydictionary.adapter;
Import COM.EXAMPLE.HAPPYDICTIONARY.R;
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.CursorAdapter;
Import Android.widget.TextView;
Custom Adapter Class
public class Dictionaryadapter extends CursorAdapter {
Private Layoutinflater Layoutinflater;
@Override
Public charsequence converttostring (cursor cursor) {
return cursor = = null? "": cursor.getstring (cursor
. Getcolumnindex ("_id"));
}
Display word information in a list
private void Setview (view view, cursor cursor) {
TextView Tvworditem = (TextView) view;
Tvworditem.settext (Cursor.getstring (Cursor.getcolumnindex ("_id"));
}
Binding options to the list
@Override
public void BindView (view view, context context, cursor cursor) {
Setview (view, cursor);
}
To generate new options
@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 Dictionaryadapter (context context, Cursor C, Boolean autorequery) {
Super (context, C, autorequery);
Layoutinflater = (layoutinflater) context
. Getsystemservice (Context.layout_inflater_service);
}
}
Worddac.java
Package Com.example.happydictionary.dao;
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Com.example.happydictionary.db.DBHelper;
public class Worddao {
Private DBHelper DBHelper;
Private Sqlitedatabase sqlitedatabase;
Public Worddao (Context context) {
Dbhelper=new DBHelper (context);
}
public string Getchinese (string 中文版) {
Sqlitedatabase=dbhelper.opendatabase ();
String sql= "Select Chinese from t_words where english=?";
Cursor cursor=sqlitedatabase.rawquery (SQL, new String[]{english});
String chinese= "no such word";
if (Cursor.movetofirst ()) {
Chinese=cursor.getstring (Cursor.getcolumnindex ("Chinese"));
}
return Chinese;
}
}
Dbhelper.java
Package com.example.happydictionary.db;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import COM.EXAMPLE.HAPPYDICTIONARY.R;
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import android.os.Environment;
Import Android.util.Log;
/**
* To copy the database files from the raw directory to the location where the database is stored in the phone
*
* @author Cabbage
*/
public class DBHelper {
Private final int buffer_size = 400000;
public static final String name = "Name";
public static final String db_name = "idiom.db"; Saved database file name
public static final String package_name = "com.example.happydictionary";//application's package name
public static final String Db_path = "/data"
+ environment.getdatadirectory (). GetAbsolutePath () + "/"
+ package_name + "/databases";
/*//SDcard Define the storage path for the database
Private final String Database_path = android.os.Environment
. getExternalStorageDirectory (). GetAbsolutePath () + "/dictionary"; */
Private context context;
Public DBHelper (Context context) {
This.context = context;
}
Public Sqlitedatabase OpenDatabase () {
try {
File Mydatapath = new file (Db_path);
if (!mydatapath.exists ()) {
Mydatapath.mkdirs ();//If no this directory is created
}
String dbfile = Mydatapath + "/" + db_name;
if (! ( New file (DBFile). Exists ())) {//Determine if the database file exists or import if it does not exist, or open the database directly
InputStream is = Context.getresources (). Openrawresource (
R.raw.dictionary);//Database to import
FileOutputStream fos = new FileOutputStream (dbfile);
byte[] buffer = new Byte[buffer_size];
int count = 0;
while ((count = is.read (buffer)) > 0) {
fos.write (buffer, 0, count);
}
Fos.close ();
Is.close ();
}
Sqlitedatabase db = Sqlitedatabase.openorcreatedatabase (DBFile,
null);
return DB;
} catch (FileNotFoundException e) {
LOG.E ("Database", "File not Found");
E.printstacktrace ();
} catch (IOException e) {
LOG.E ("Database", "IO exception");
E.printstacktrace ();
}
return null;
}
Public Cursor query (String name) {
Sqlitedatabase db = This.getreadabledatabase ();
Return Db.rawquery ("select * from T_words where 中文版 like '%" + name + "% ' limit", null);
}
Private Sqlitedatabase getreadabledatabase () {
TODO auto-generated Method Stub
return null;
}
}
Word.java
Package com.example.happydictionary.entity;
public class Word {
Private String 中文版;
Private String Chinese;
Public String Getenglish () {
return 中文版;
}
public void Setenglish (String 中文版) {
This.english = 中文版;
}
Public String Getchinese () {
return Chinese;
}
public void Setchinese (String Chinese) {
This.chinese = Chinese;
}
}
Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android:background= "@drawable/mainbg"
android:paddingbottom= "@dimen/activity_vertical_margin"
Android: paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= ". Mainactivity "
<button
android:id=" @+id/btnsearch "
android:layout_width=" Wrap_content "
android:layout_height= "Wrap_content"
android:layout_alignbaseline= "@+id/etword"
Android:layout_ Alignbottom= "@+id/etword"
android:layout_marginleft= "16DP"
android:layout_torightof= "@+id/etword"
android:background= "@drawable/ibsearchword"
android:onclick= "Searchword"
android:text= "@string/ Serachword "/>
<textview
Android:id= "@+id/tvsearchresult"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:layout_alignleft= "@+id/etword"
android:layout_below= "@+id/etword"
android:layout_margintop= "22DP"
Android:textsize= "25SP"
android:background= "@drawable/bg_roundcorner"
Android:textappearance= "? Android:attr/textappearancemedium"/>
<autocompletetextview
Android:id= "@+id/etword"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true"
android:layout_margintop= "31DP"
android:background= "@android:d rawable/edit_text"
Android:ems= "10"
android:hint= "@string/searchhint"
Android:singleline= "true"
Android:textcolor= "#552006"
Android:textcolorhint= "#782f10" >
<requestfocus/>
</AutoCompleteTextView>
</RelativeLayout>
Word_list_item.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<textview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/tvworditem"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:gravity= "Center_vertical"
android:minheight= "? Android:attr/listpreferreditemheight"
android:paddingleft= "6dip"
Android:textappearance= "? Android:attr/textappearancelarge"
Android:textcolor= "@color/gray"/>
Android Auto Completetexview