全网整合营销服务商

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

免费咨询热线: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 


相关文章: 盘锦网站制作公司,盘锦大洼有多少5G网站?  如何获取开源自助建站系统免费下载链接?  如何设置并定期更换建站之星安全管理员密码?  官网自助建站平台指南:在线制作、快速建站与模板选择全解析  如何用搬瓦工VPS快速搭建个人网站?  如何在Golang中使用encoding/gob序列化对象_存储和传输数据  青岛网站建设如何选择本地服务器?  建站之星手机一键生成:多端自适应+小程序开发快速建站指南  如何访问已购建站主机并解决登录问题?  独立制作一个网站多少钱,建立网站需要花多少钱?  如何快速查询网站的真实建站时间?  如何在西部数码注册域名并快速搭建网站?  大连网站制作费用,大连新青年网站,五年四班里的视频怎样下载啊?  如何用腾讯建站主机快速创建免费网站?  小程序网站制作需要准备什么资料,如何制作小程序?  高端建站三要素:定制模板、企业官网与响应式设计优化  电脑免费海报制作网站推荐,招聘海报哪个网站多?  广州网站建站公司选择指南:建站流程与SEO优化关键词解析  如何基于云服务器快速搭建网站及云盘系统?  香港服务器租用每月最低只需15元?  怎么将XML数据可视化 D3.js加载XML  安徽网站建设与外贸建站服务专业定制方案  如何零基础在云服务器搭建WordPress站点?  建站上传速度慢?如何优化加速网站加载效率?  如何将凡科建站内容保存为本地文件?  简历在线制作网站免费版,如何创建个人简历?  行程制作网站有哪些,第三方机票电子行程单怎么开?  建站之星备案是否影响网站上线时间?  美食网站链接制作教程视频,哪个教做美食的网站比较专业点?  营销式网站制作方案,销售哪个网站招聘效果最好?  企业微网站怎么做,公司网站和公众号有什么区别?  制作网站怎么制作,*游戏网站怎么搭建?  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  建站之星如何快速更换网站模板?  网站制作公司排行榜,四大门户网站排名?  黑客如何通过漏洞一步步攻陷网站服务器?  建站之星后台管理:高效配置与模板优化提升用户体验  如何做网站制作流程,*游戏网站怎么搭建?  网站制作网站,深圳做网站哪家比较好?  怎么用手机制作网站链接,dw怎么把手机适应页面变成网页?  如何通过智能用户系统一键生成高效建站方案?  一键制作网站软件下载安装,一键自动采集网页文档制作步骤?  外贸公司网站制作哪家好,maersk船公司官网?  专业制作网站的公司哪家好,建立一个公司网站的费用.有哪些部分,分别要多少钱?  长沙做网站要多少钱,长沙国安网络怎么样?  免费网站制作appp,免费制作app哪个平台好?  盐城做公司网站,江苏电子版退休证办理流程?  已有域名建站全流程解析:网站搭建步骤与建站工具选择  建站主机功能解析:服务器选择与快速搭建指南  教学论文网站制作软件有哪些,写论文用什么软件 ? 

您的项目需求

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