国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android+SQLite數(shù)據(jù)庫實現(xiàn)的生詞記事本功能實例

2019-12-12 01:59:57
字體:
來源:轉載
供稿:網(wǎng)友

本文實例講述了Android+SQLite數(shù)據(jù)庫實現(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結果集    while(cursor.moveToNext())    {      //將結果集中的數(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();    //退出程序時關閉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>

另一個需要跳轉的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的子項目布局命名為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);  }}

更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android操作SQLite數(shù)據(jù)庫技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》、《Android編程之a(chǎn)ctivity操作技巧總結》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 中阳县| 东乌| 武清区| 青河县| 新民市| 高唐县| 新昌县| 阜城县| 多伦县| 泰安市| 中宁县| 建阳市| 贵德县| 丹东市| 无极县| 中山市| 噶尔县| 岗巴县| 定日县| 康定县| 普安县| 绩溪县| 馆陶县| 诏安县| 安远县| 育儿| 邵阳县| 太仓市| 沁源县| 图们市| 偃师市| 垦利县| 陆丰市| 额济纳旗| 三亚市| 蓬安县| 黔南| 桐梓县| 威海市| 新乡县| 万年县|