1. First, write a Database Help class, as shown below:
package com.example.demonewword;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class MyDataBaseHelper extends SQLiteOpenHelper {public MyDataBaseHelper(Context context, String name, int version) {super(context, name, null, version);}@Overridepublic void onCreate(SQLiteDatabase db) {String sql = "CREATE TABLE dict(_id INTEGER PRIMARY KEY autoincrement ,word,detail)";db.execSQL(sql);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}
2. Write the layout file. Because this program focuses on exercises, the layout is relatively simple, as shown below:
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: orientation = "vertical"> <edittext Android: Id = "@ + ID/edittext1" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: EMS = "10" Android: hint = "Enter the word"> <requestfocus/> </edittext> <edittext Android: Id = "@ + ID/edittext2" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: EMS = "10" Android: hint = "Enter the meaning of the word"/> <button Android: id = "@ + ID/button1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Add new words"/> <edittext Android: id = "@ + ID/edittext3" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: EMS = "10" Android: hint = "Enter the word to search"/> <button Android: Id = "@ + ID/button2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = ""/> <listview Android: Id = "@ + ID/listview1" Android: layout_width = "match_parent" Android: layout_height = "wrap_content"> </listview> </linearlayout>
3. A Layout file is also required in listview. The line. xml file is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>
4. Main Java code of the program is as follows:
Package COM. example. demonewword; import Java. util. arraylist; import Java. util. hashmap; import Java. util. map; import android. app. activity; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. Widget. listview; import android. widget. simpleadapter; import android. widget. toast; public class mainactivity extends activity {private edittext word; private edittext wordmean; private button searchbut; private button insertbut; private edittext input; private listview; private mydatabasehelper dbhelper; private sqlitedatabase sqldb; @ overridepublic void oncreate (bundle savedinstancestate) {super. on Create (savedinstancestate); setcontentview (R. layout. activity_main); word = (edittext) findviewbyid (R. id. edittext1); wordmean = (edittext) findviewbyid (R. id. edittext2); input = (edittext) findviewbyid (R. id. edittext3); insertbut = (button) findviewbyid (R. id. button1); searchbut = (button) findviewbyid (R. id. button2); listview = (listview) findviewbyid (R. id. listview1); dbhelper = new mydatabasehelper (mainactiv Ity. this, "mydict. db3 ", 1); insertbut. setonclicklistener (New onclicklistener () {public void onclick (view v) {insertmethod () ;}}); searchbut. setonclicklistener (New onclicklistener () {public void onclick (view v) {searchmethod () ;}) ;}// insert the word protected void insertmethod () {string strword = word. gettext (). tostring (); string strmean = wordmean. gettext (). tostring (); // Insert the new word record sqldb = dbhelper. getreadabledatabas E(;;sqldb.exe csql ("insert into dict values (null ,?,?) ", New string [] {strword, strmean}); toast. maketext (getapplicationcontext (), "inserted successfully", toast. length_long ). show () ;}// find the word protected void searchmethod () {string key = input. gettext (). tostring (); cursor = dbhelper. getreadabledatabase (). rawquery ("select * From dict where word like? Or detail like? ", New string [] {" % "+ key +" % "," % "+ key +" % "}); arraylist <Map <string, string> result = new arraylist <Map <string, string> (); While (cursor. movetonext () {Map <string, string> map = new hashmap <string, string> (); map. put ("Word", cursor. getstring (1); map. put ("detail", cursor. getstring (2); result. add (MAP);} simpleadapter = new simpleadapter (getapplicationcontext (), result, R. layout. line, new string [] {"Word", "detail"}, new int [] {R. id. textview1, R. id. textview2}); listview. setadapter (simpleadapter) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}
So far, the program can run perfectly, and a simple word notebook will come out.