Android+SQLite數(shù)據(jù)庫實(shí)現(xiàn)的生詞記事本功能實(shí)例
本文實(shí)例講述了Android+SQLite數(shù)據(jù)庫實(shí)現(xiàn)的生詞記事本功能。分享給大家供大家參考,具體如下:
主activity命名為
Dict:
代碼如下:
package example.com.myapplication; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class Dict extends Activity { MyDatabaseHelper dbHelper; Button insert = null; Button search = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 創(chuàng)建MyDatabaseHelper對象,指定數(shù)據(jù)庫版本為1,此處使用相對路徑即可, // 數(shù)據(jù)庫文件自動會保存在程序的數(shù)據(jù)文件夾的databases目錄下。 dbHelper = new MyDatabaseHelper(this , "myDict.db3" , 1); insert = (Button)findViewById(R.id.insert); search = (Button)findViewById(R.id.search); insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View source) { //獲取用戶輸入 String word = ((EditText)findViewById(R.id.word)) .getText().toString(); String detail = ((EditText)findViewById(R.id.detail)) .getText().toString(); //插入生詞記錄 insertData(dbHelper.getReadableDatabase() , word , detail); //顯示提示信息 Toast.makeText(Dict.this, "添加生詞成功!" , Toast.LENGTH_SHORT) .show(); } }); search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View source) { // 獲取用戶輸入 String key = ((EditText) findViewById(R.id.key)).getText() .toString(); // 執(zhí)行查詢 Cursor cursor = dbHelper.getReadableDatabase().rawQuery( "select * from dict where word like ? or detail like ?", new String[]{"%" + key + "%" , "%" + key + "%"}); //創(chuàng)建一個Bundle對象 Bundle data = new Bundle(); data.putSerializable("data", converCursorToList(cursor)); //創(chuàng)建一個Intent Intent intent = new Intent(Dict.this , ResultActivity.class); intent.putExtras(data); //啟動Activity startActivity(intent); } }); } protected ArrayList<Map<String ,String>> converCursorToList(Cursor cursor) { ArrayList<Map<String,String>> result = new ArrayList<Map<String ,String>>(); //遍歷Cursor結(jié)果集 while(cursor.moveToNext()) { //將結(jié)果集中的數(shù)據(jù)存入ArrayList中 Map<String, String> map = new HashMap<String,String>(); //取出查詢記錄中第2列、第3列的值 map.put("word" , cursor.getString(1)); map.put("detail" , cursor.getString(2)); result.add(map); } return result; } private void insertData(SQLiteDatabase db , String word , String detail) { //執(zhí)行插入語句 db.execSQL("insert into dict values(null , ? , ?)" , new String[]{word , detail}); } @Override public void onDestroy() { super.onDestroy(); //退出程序時關(guān)閉MyDatabaseHelper里的SQLiteDatabase if (dbHelper != null) { dbHelper.close(); } } }
他的布局文件activity_main代碼如下:
<!--?xml version="1.0" encoding="utf-8"?--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <EditText android:id="@+id/word" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/input"/> <EditText android:id="@+id/detail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/input" android:lines="3"/> <Button android:id="@+id/insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/insert"/> <EditText android:id="@+id/key" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/record"/> <Button android:id="@+id/search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/search"/> <ListView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
另一個需要跳轉(zhuǎn)的activity命名為:
ResultActivity
具體代碼如下:
package example.com.myapplication; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.List; import java.util.Map; public class ResultActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.popup); ListView listView = (ListView)findViewById(R.id.show); Intent intent = getIntent(); //獲取該intent所攜帶的數(shù)據(jù) Bundle data = intent.getExtras(); //從Bundle數(shù)據(jù)包中取出數(shù)據(jù) @SuppressWarnings("unchecked") List<Map<String,String>> list = (List<Map<String ,String>>)data.getSerializable("data"); //將List封裝成SimpleAdapter SimpleAdapter adapter = new SimpleAdapter( ResultActivity.this , list , R.layout.ine , new String[]{"word" , "detail"} , new int[]{R.id.my_title , R.id.my_content}); //填充ListView listView.setAdapter(adapter); } }
他的布局文件命名為popup: 代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment"> <TextView android:id="@+id/my_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/my_content" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
listView的子項(xiàng)目布局命名為ine:
代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment"> <TextView android:id="@+id/my_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/my_content" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
最后數(shù)據(jù)庫幫助類命名為:
MyDatabaseHelper:
代碼如下:
package example.com.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyDatabaseHelper extends SQLiteOpenHelper { final String CREATE_TABLE_SQL = "create table dict(_id integer primary key autoincrement , word , detail)"; public MyDatabaseHelper(Context context, String name, int version) { super(context, name, null, version); } @Override public void onCreate(SQLiteDatabase db) { // 第一個使用數(shù)據(jù)庫時自動建表 db.execSQL(CREATE_TABLE_SQL); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("--------onUpdate Called--------" + oldVersion + "--->" + newVersion); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
這篇文章主要介紹了Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小的相關(guān)代碼,需要的朋友可以參考下2015-09-09Android仿抖音右滑清屏左滑列表功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿抖音右滑清屏左滑列表功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Android廣播實(shí)現(xiàn)App開機(jī)自啟動
這篇文章主要為大家詳細(xì)介紹了Android廣播實(shí)現(xiàn)App開機(jī)自啟動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android協(xié)程作用域與序列發(fā)生器限制介紹梳理
協(xié)程的作用是什么?協(xié)程是一種輕量級的線程,解決異步編程的復(fù)雜性,異步的代碼使用協(xié)程可以用順序進(jìn)行表達(dá),文中通過示例代碼介紹詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-08-08Android實(shí)現(xiàn)引導(dǎo)頁的圓點(diǎn)指示器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)引導(dǎo)頁的圓點(diǎn)指示器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Android倒計(jì)時控件 Splash界面5秒自動跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android倒計(jì)時控件,Splash界面5秒自動跳轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09