Android+SQLite数据库实现的生词记事本功能实例
发布时间 - 2026-01-11 03:24:03 点击率:次本文实例讲述了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
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
详解Nginx + Tomcat 反向代理 负载均衡 集群 部署指南
如何在服务器上配置二级域名建站?
Laravel Sail是什么_基于Docker的Laravel本地开发环境Sail入门
独立制作一个网站多少钱,建立网站需要花多少钱?
ChatGPT 4.0官网入口地址 ChatGPT在线体验官网
浏览器如何快速切换搜索引擎_在地址栏使用不同搜索引擎【搜索】
Laravel怎么生成URL_Laravel路由命名与URL生成函数详解
Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】
Laravel如何实现用户角色和权限系统_Laravel角色权限管理机制
Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件
Laravel如何实现用户密码重置功能?(完整流程代码)
Laravel API资源(Resource)怎么用_格式化Laravel API响应的最佳实践
Google浏览器为什么这么卡 Google浏览器提速优化设置步骤【方法】
免费网站制作appp,免费制作app哪个平台好?
标题:Vue + Vuex 项目中正确使用 JWT 进行身份认证的实践指南
javascript事件捕获机制【深入分析IE和DOM中的事件模型】
Laravel如何使用Facades(门面)及其工作原理_Laravel门面模式与底层机制
Java垃圾回收器的方法和原理总结
LinuxShell函数封装方法_脚本复用设计思路【教程】
如何快速辨别茅台真假?关键步骤解析
如何在建站之星网店版论坛获取技术支持?
如何快速生成高效建站系统源代码?
米侠浏览器网页图片不显示怎么办 米侠图片加载修复
Laravel如何使用Eloquent进行子查询
如何安全更换建站之星模板并保留数据?
如何实现javascript表单验证_正则表达式有哪些实用技巧
百度输入法ai面板怎么关 百度输入法ai面板隐藏技巧
如何用低价快速搭建高质量网站?
Swift中swift中的switch 语句
Laravel队列由Redis驱动怎么配置_Laravel Redis队列使用教程
Laravel如何实现一对一模型关联?(Eloquent示例)
JavaScript模板引擎Template.js使用详解
如何用搬瓦工VPS快速搭建个人网站?
Python并发异常传播_错误处理解析【教程】
高配服务器限时抢购:企业级配置与回收服务一站式优惠方案
什么是JavaScript解构赋值_解构赋值有哪些实用技巧
潮流网站制作头像软件下载,适合母子的网名有哪些?
Win11怎样安装网易有道词典_Win11安装词典教程【步骤】
Laravel安装步骤详细教程_Laravel环境搭建指南
Laravel怎么写单元测试_PHPUnit在Laravel项目中的基础测试入门
如何在万网自助建站平台快速创建网站?
php做exe能调用系统命令吗_执行cmd指令实现方式【详解】
如何在阿里云虚拟主机上快速搭建个人网站?
jimdo怎样用html5做选项卡_jimdo选项卡html5实现与切换效果【指南】
香港服务器WordPress建站指南:SEO优化与高效部署策略
Laravel storage目录权限问题_Laravel文件写入权限设置
JavaScript如何操作视频_媒体API怎么控制播放
邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
网站优化排名时,需要考虑哪些问题呢?

