Android offline dictionary

Source: Internet
Author: User

1. First import the file dictionary. DB/files/lee0oo0/dictionary.rar in res/raw.

2. Main. xml file Layout

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

<Autocompletetextview Android: Id = "@ + ID/actvword"
Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"
Android: layout_margintop = "10dp" Android: singleline = "true"/>
<Button Android: Id = "@ + ID/btnselectword" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = ""/>

3. The drop-down layout of each textview in autocompletetextview

<? 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: textappearance = "? Android: ATTR/textappearancelarge"
Android: gravity = "center_vertical"
Android: paddingleft = "6dip"
Android: textcolor = "#000"
Android: minheight = "? Android: ATTR/listpreferreditemheight "/>

 

4. The main program is almost Annotated

Package net. blogjava. Mobile;

Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import Android. App. activity;
Import Android. App. alertdialog;
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. View. layoutinflater;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. View. View. onclicklistener;
Import Android. widget. autocompletetextview;
Import Android. widget. Button;
Import Android. widget. cursoradapter;
Import Android. widget. textview;

Public class main extends activity implements onclicklistener, textwatcher
{
// Storage directory of dictionary. DB
Private final string database_path = Android. OS. Environment
. Getexternalstoragedirectory (). getabsolutepath ()
+ "/Dictionary ";
Private autocompletetextview actvword;
Private final string database_filename = "dictionary. DB ";
Private sqlitedatabase database;
Private button btnselectword;

@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );
// Open the database
Database = opendatabase ();
Btnselectword = (button) findviewbyid (R. Id. btnselectword );
Actvword = (autocompletetextview) findviewbyid (R. Id. actvword );
// Set button listening
Btnselectword. setonclicklistener (this );
// Facility character change listener
Actvword. addtextchangedlistener (this );
}

Public class dictionaryadapter extends cursoradapter
{
Private layoutinflater;
@ Override
Public charsequence converttostring (cursor)
{
Return cursor = NULL? "": Cursor. getstring (cursor
. Getcolumnindex ("_ id "));
}

Private void setview (view, cursor)
{
Textview tvworditem = (textview) view;
Tvworditem. settext (cursor. getstring (cursor. getcolumnindex ("_ id ")));
}

@ Override
Public void BindView (view, context, cursor)
{
Setview (view, cursor );
}

@ Override
Public View newview (context, cursor, viewgroup parent)
{
// Convert the layout file to a view object
View view = layoutinflater. Inflate (R. layout. word_list_item, null );
Setview (view, cursor );
Return view;
}

Public dictionaryadapter (context, cursor C, Boolean autorequery)
{
Super (context, C, autorequery );
// Obtain the layoutinflater object of the context through the System Service
Layoutinflater = (layoutinflater) Context
. Getsystemservice (context. layout_inflater_service );
}
}

Public void aftertextchanged (editable S)
{

// The alias of the English field must be set to _ id
Cursor cursor = database. rawquery (
"Select English as _ id from t_words where English like? ",
New String []
{S. tostring () + "% "});
Dictionaryadapter = new dictionaryadapter (this,
Cursor, true );
Actvword. setadapter (dictionaryadapter );

}

Public void beforetextchanged (charsequence S, int start, int count,
Int after)
{
// Todo auto-generated method stub

}

Public void ontextchanged (charsequence S, int start, int before, int count)
{
// Todo auto-generated method stub

}

Public void onclick (view)
{
// Search for English from the t_words table as Chinese in the input box
String SQL = "select Chinese from t_words where English =? ";
Cursor cursor = database. rawquery (SQL, new string []
{Actvword. gettext (). tostring ()});
String result = "this word is not found .";
// Display the Chinese meaning of a word if it is searched
If (cursor. getcount ()> 0)
{
// The movetofirst method must be used to move the record pointer to the position of 1st records
Cursor. movetofirst ();
Result = cursor. getstring (cursor. getcolumnindex ("Chinese "));
}
// Display the query result dialog box
New alertdialog. Builder (this). settitle ("query result"). setmessage (result)
. Setpositivebutton ("off", null). Show ();

}

Private sqlitedatabase opendatabase ()
{
Try
{
// Obtain the absolute path of the dictionary. DB File
String databasefilename = database_path + "/" + database_filename;
File dir = new file (database_path );
// If the/sdcard/dictionary directory exists, create this directory
If (! Dir. exists ())
Dir. mkdir ();
// If the directory does not exist in the/sdcard/dictionary directory
// Dictionary. DB file, copy this file from the res \ raw directory
// Directory of the SD card (/sdcard/dictionary)
If (! (New file (databasefilename). exists ())
{
// Obtain the inputstream object that encapsulates the dictionary. DB File
Inputstream is = getresources (). openrawresource (
R. Raw. dictionary );
Fileoutputstream Fos = new fileoutputstream (databasefilename );
Byte [] buffer = new byte [8192];
Int COUNT = 0;
// Start copying the dictionary. DB File
While (COUNT = is. Read (buffer)> 0)
{
FOS. Write (buffer, 0, count );
}

FOS. Close ();
Is. Close ();
}
// Open the dictionary. DB file in the/sdcard/dictionary directory
Sqlitedatabase database = sqlitedatabase. openorcreatedatabase (
Databasefilename, null );
Return database;
}
Catch (exception E)
{
}
Return NULL;
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.