全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Android+SQLite数据库实现的生词记事本功能实例

本文实例讲述了Android+SQLite数据库实现的生词记事本功能。分享给大家供大家参考,具体如下:

主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);
    // 创建MyDatabaseHelper对象,指定数据库版本为1,此处使用相对路径即可,
    // 数据库文件自动会保存在程序的数据文件夹的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();
        // 执行查询
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(
            "select * from dict where word like ? or detail like ?",
            new String[]{"%" + key + "%" , "%" + key + "%"});
        //创建一个Bundle对象
        Bundle data = new Bundle();
        data.putSerializable("data", converCursorToList(cursor));
        //创建一个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())
    {
      //将结果集中的数据存入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)
  {
    //执行插入语句
    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所携带的数据
    Bundle data = intent.getExtras();
    //从Bundle数据包中取出数据
    @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>

最后数据库帮助类命名为:

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)
  {
    // 第一个使用数据库时自动建表
    db.execSQL(CREATE_TABLE_SQL);
  }
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  {
    System.out.println("--------onUpdate Called--------"
        + oldVersion + "--->" + newVersion);
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android操作SQLite数据库技巧总结》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android文件操作技巧汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。


# Android  # SQLite  # 数据库  # 生词  # 记事本  # Android实现简易记事本  # Android实现记事本小功能  # Android记事本项目开发  # Android实现记事本功能  # android实现记事本app  # Android中实现记事本动态添加行效果  # Android实现记事本功能(26)  # Android利用Intent实现记事本功能(NotePad)  # Android手机开发设计之记事本功能  # 命名为  # 操作技巧  # 创建一个  # 进阶  # 相关内容  # 第一个  # 遍历  # 感兴趣  # 提示信息  # 给大家  # 跳转  # 更多关于  # 所述  # 程序设计  # 包中  # 数据库文件  # 目录下  # 讲述了  # databases  # System 


相关文章: ,如何利用word制作宣传手册?  韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐  在线流程图制作网站手机版,谁能推荐几个好的CG原画资源网站么?  专业制作网站的公司哪家好,建立一个公司网站的费用.有哪些部分,分别要多少钱?  建站之星安装路径如何正确选择及配置?  高端网站建设与定制开发一站式解决方案 中企动力  无锡营销型网站制作公司,无锡网选车牌流程?  宝塔建站教程:一键部署配置流程与SEO优化实战指南  如何在云主机上快速搭建多站点网站?  音响网站制作视频教程,隆霸音响官方网站?  洛阳网站制作公司有哪些,洛阳的招聘网站都有哪些?  微信推文制作网站有哪些,怎么做微信推文,急?  如何用AWS免费套餐快速搭建高效网站?  建设网站制作价格,怎样建立自己的公司网站?  如何在阿里云通过域名搭建网站?  已有域名和空间,如何快速搭建网站?  浙江网站制作公司有哪些,浙江栢塑信息技术有限公司定制网站做的怎么样?  建站之星Pro快速搭建教程:模板选择与功能配置指南  建站主机服务器选购指南:轻量应用与VPS配置解析  香港网站服务器数量如何影响SEO优化效果?  代购小票制作网站有哪些,购物小票的简要说明?  深圳网站制作的公司有哪些,dido官方网站?  如何在阿里云部署织梦网站?  外贸公司网站制作哪家好,maersk船公司官网?  代刷网站制作软件,别人代刷火车票靠谱吗?  建站主机无法访问?如何排查域名与服务器问题  如何安全更换建站之星模板并保留数据?  免费制作小说封面的网站有哪些,怎么接网站批量的封面单?  招贴海报怎么做,什么是海报招贴?  建站主机CVM配置优化、SEO策略与性能提升指南  建站之星IIS配置教程:代码生成技巧与站点搭建指南  上海网站制作网页,上海本地的生活网站有哪些?最好包括生活的各个方面的?  c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】  建站之星CMS五站合一模板配置与SEO优化指南  如何在自有机房高效搭建专业网站?  电商网站制作价格怎么算,网上拍卖流程以及规则?  如何通过老薛主机一键快速建站?  青岛网站建设如何选择本地服务器?  江苏网站制作公司有哪些,江苏书法考级官方网站?  如何实现建站之星域名转发设置?  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  导航网站建站方案与优化指南:一站式高效搭建技巧解析  如何用好域名打造高点击率的自主建站?  详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)  C++中引用和指针有什么区别?(代码说明)  python的本地网站制作,如何创建本地站点?  宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?  建站之星代理平台如何选择最佳方案?  nginx修改上传文件大小限制的方法  如何用花生壳三步快速搭建专属网站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。