欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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è)計(jì)有所幫助。

相關(guān)文章

  • Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎ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-09
  • Android實(shí)現(xiàn)粒子漩渦動畫

    Android實(shí)現(xiàn)粒子漩渦動畫

    粒子動畫經(jīng)常用于大畫幅的渲染效果,實(shí)際上難度并不高,但是在使用粒子動畫時,必須要遵循的一些要素,起點(diǎn),矢量速度和符合運(yùn)動學(xué)公式等,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下
    2024-01-01
  • Android仿抖音右滑清屏左滑列表功能的實(shí)現(xiàn)代碼

    Android仿抖音右滑清屏左滑列表功能的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android仿抖音右滑清屏左滑列表功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Android廣播實(shí)現(xiàn)App開機(jī)自啟動

    Android廣播實(shí)現(xiàn)App開機(jī)自啟動

    這篇文章主要為大家詳細(xì)介紹了Android廣播實(shí)現(xiàn)App開機(jī)自啟動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android協(xié)程作用域與序列發(fā)生器限制介紹梳理

    Android協(xié)程作用域與序列發(fā)生器限制介紹梳理

    協(xié)程的作用是什么?協(xié)程是一種輕量級的線程,解決異步編程的復(fù)雜性,異步的代碼使用協(xié)程可以用順序進(jìn)行表達(dá),文中通過示例代碼介紹詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-08-08
  • Kotlin掛起函數(shù)的詳細(xì)介紹

    Kotlin掛起函數(shù)的詳細(xì)介紹

    掛起函數(shù)用狀態(tài)機(jī)以掛起點(diǎn)將協(xié)程的運(yùn)算邏輯拆分成不同的片段,每次執(zhí)行協(xié)程運(yùn)行不同的邏輯片段,由此可以知道協(xié)程是運(yùn)行在線程中的,線程的并發(fā)處理方式也可以用在協(xié)程上
    2022-09-09
  • 利用Flutter制作一個摸魚桌面版App

    利用Flutter制作一個摸魚桌面版App

    Win10商店上架了一款名為《摸魚》的App,在下載打開之后,這個App會讓你的電腦進(jìn)入一個假更新的畫面。本文將為大家介紹如何通過Flutter制作一個桌面版的摸魚APP,快跟小編一起學(xué)習(xí)一下吧
    2021-12-12
  • android中intent傳遞list或者對象的方法

    android中intent傳遞list或者對象的方法

    這篇文章主要介紹了android中intent傳遞list或者對象的方法,分析羅列了常用的幾種方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • Android實(shí)現(xiàn)引導(dǎo)頁的圓點(diǎn)指示器

    Android實(shí)現(xiàn)引導(dǎo)頁的圓點(diǎn)指示器

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)引導(dǎo)頁的圓點(diǎn)指示器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Android倒計(jì)時控件 Splash界面5秒自動跳轉(zhuǎn)

    Android倒計(jì)時控件 Splash界面5秒自動跳轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了Android倒計(jì)時控件,Splash界面5秒自動跳轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09

最新評論