Android+SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)的生詞記事本功能實(shí)例
本文實(shí)例講述了Android+SQLite數(shù)據(jù)庫(kù)實(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對(duì)象,指定數(shù)據(jù)庫(kù)版本為1,此處使用相對(duì)路徑即可, // 數(shù)據(jù)庫(kù)文件自動(dòng)會(huì)保存在程序的數(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)建一個(gè)Bundle對(duì)象 Bundle data = new Bundle(); data.putSerializable("data", converCursorToList(cursor)); //創(chuàng)建一個(gè)Intent Intent intent = new Intent(Dict.this , ResultActivity.class); intent.putExtras(data); //啟動(dòng)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í)行插入語(yǔ)句 db.execSQL("insert into dict values(null , ? , ?)" , new String[]{word , detail}); } @Override public void onDestroy() { super.onDestroy(); //退出程序時(shí)關(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>
另一個(gè)需要跳轉(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ù)庫(kù)幫助類(lèi)命名為:
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) { // 第一個(gè)使用數(shù)據(jù)庫(kù)時(shí)自動(dòng)建表 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)容感興趣的讀者可查看本站專(zhuān)題:《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)簡(jiǎn)易記事本
- Android實(shí)現(xiàn)記事本小功能
- Android記事本項(xiàng)目開(kāi)發(fā)
- Android實(shí)現(xiàn)記事本功能
- Android實(shí)現(xiàn)簡(jiǎn)易記事本
- android實(shí)現(xiàn)記事本app
- Android中實(shí)現(xiàn)記事本動(dòng)態(tài)添加行效果
- Android實(shí)現(xiàn)記事本功能(26)
- Android利用Intent實(shí)現(xiàn)記事本功能(NotePad)
- Android手機(jī)開(kāi)發(fā)設(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)粒子漩渦動(dòng)畫(huà)
粒子動(dòng)畫(huà)經(jīng)常用于大畫(huà)幅的渲染效果,實(shí)際上難度并不高,但是在使用粒子動(dòng)畫(huà)時(shí),必須要遵循的一些要素,起點(diǎn),矢量速度和符合運(yùn)動(dòng)學(xué)公式等,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-01-01Android仿抖音右滑清屏左滑列表功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿抖音右滑清屏左滑列表功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Android廣播實(shí)現(xiàn)App開(kāi)機(jī)自啟動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android廣播實(shí)現(xiàn)App開(kāi)機(jī)自啟動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android協(xié)程作用域與序列發(fā)生器限制介紹梳理
協(xié)程的作用是什么?協(xié)程是一種輕量級(jí)的線程,解決異步編程的復(fù)雜性,異步的代碼使用協(xié)程可以用順序進(jìn)行表達(dá),文中通過(guò)示例代碼介紹詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-08-08android中intent傳遞list或者對(duì)象的方法
這篇文章主要介紹了android中intent傳遞list或者對(duì)象的方法,分析羅列了常用的幾種方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01Android實(shí)現(xiàn)引導(dǎo)頁(yè)的圓點(diǎn)指示器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)引導(dǎo)頁(yè)的圓點(diǎn)指示器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Android倒計(jì)時(shí)控件 Splash界面5秒自動(dòng)跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android倒計(jì)時(shí)控件,Splash界面5秒自動(dòng)跳轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09