最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年09月25日 10:59:13   作者:ITzhongzi  
這篇文章主要介紹了Android+SQLite數(shù)據(jù)庫實(shí)現(xiàn)的生詞記事本功能,結(jié)合具體實(shí)例形式分析了Android操作SQLite數(shù)據(jù)庫實(shí)現(xiàn)生詞記錄功能的操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(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è)計有所幫助。

相關(guān)文章

  • 實(shí)例詳解Android Selector和Shape的用法

    實(shí)例詳解Android Selector和Shape的用法

    shape和selector是Android UI設(shè)計中經(jīng)常用到的,比如我們要自定義一個圓角Button,點(diǎn)擊Button有些效果的變化,就要用到shape和selector,通過本文結(jié)合代碼實(shí)例給大家詳解Android Selector和Shape的用法,感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • Android 中 ActivityLifecycleCallbacks的實(shí)例詳解

    Android 中 ActivityLifecycleCallbacks的實(shí)例詳解

    這篇文章主要介紹了Android 中 ActivityLifecycleCallbacks的實(shí)例詳解的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Android編程之MD5加密算法實(shí)例分析

    Android編程之MD5加密算法實(shí)例分析

    這篇文章主要介紹了Android編程之MD5加密算法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android使用MD5加密的具體實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Kotlin函數(shù)式編程超詳細(xì)介紹

    Kotlin函數(shù)式編程超詳細(xì)介紹

    一個函數(shù)式應(yīng)用通常由三大類函數(shù)構(gòu)成:變換transform、過濾filters合并combineo每類函數(shù)都針對集合數(shù)據(jù)類型設(shè)計,目標(biāo)是產(chǎn)生一個最終結(jié)果。函數(shù)式編程用到的函數(shù)生來都是可組合的,也就是說,你可以組合多個簡單函數(shù)來構(gòu)建復(fù)雜的計算行為
    2022-09-09
  • Android開源堆疊滑動控件仿探探效果

    Android開源堆疊滑動控件仿探探效果

    這篇文章主要為大家詳細(xì)介紹了Android開源堆疊滑動控件仿探探效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Kotlin如何優(yōu)雅地判斷EditText數(shù)據(jù)是否為空詳解

    Kotlin如何優(yōu)雅地判斷EditText數(shù)據(jù)是否為空詳解

    這篇文章主要給大家介紹了關(guān)于Kotlin如何優(yōu)雅地判斷EditText數(shù)據(jù)是否為空的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • Android Studio使用小技巧:自定義Logcat

    Android Studio使用小技巧:自定義Logcat

    這篇文章主要介紹了Android Studio使用小技巧:自定義Logcat,本文講解如何自定義Logcat的顏色,實(shí)現(xiàn)區(qū)別verbose、debug、error等分類信息,需要的朋友可以參考下
    2015-05-05
  • android 仿微信demo——微信通訊錄界面功能實(shí)現(xiàn)(移動端,服務(wù)端)

    android 仿微信demo——微信通訊錄界面功能實(shí)現(xiàn)(移動端,服務(wù)端)

    本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給你們提供幫助
    2021-06-06
  • Android 使用fast-verification實(shí)現(xiàn)驗(yàn)證碼填寫功能的實(shí)例代碼

    Android 使用fast-verification實(shí)現(xiàn)驗(yàn)證碼填寫功能的實(shí)例代碼

    這篇文章主要介紹了Android 使用fast-verification實(shí)現(xiàn)驗(yàn)證碼填寫功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Android利用Fragment實(shí)現(xiàn)Tab選項(xiàng)卡效果

    Android利用Fragment實(shí)現(xiàn)Tab選項(xiàng)卡效果

    這篇文章主要為大家詳細(xì)介紹了Android利用Fragment實(shí)現(xiàn)Tab選項(xiàng)卡效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論

定西市| 宜都市| 鄂尔多斯市| 樟树市| 保山市| 马公市| 钦州市| 衢州市| 霸州市| 阳信县| 民勤县| 诸暨市| 揭西县| 杭州市| 随州市| 新巴尔虎右旗| 鄢陵县| 南江县| 顺义区| 繁峙县| 永靖县| 朝阳区| 赤峰市| 双鸭山市| 嘉禾县| 河池市| 石河子市| 民县| 寿宁县| 新安县| 休宁县| 蒙山县| 仙桃市| 新安县| 开封县| 太和县| 石楼县| 新乐市| 三原县| 四会市| 伊川县|