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 】
【
网络技术251811 】
【
AI营销90571 】
相关推荐:
如何在香港免费服务器上快速搭建网站?
uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址
高端建站如何打造兼具美学与转化的品牌官网?
php增删改查怎么学_零基础入门php数据库操作必知基础【教程】
浅析上传头像示例及其注意事项
无锡营销型网站制作公司,无锡网选车牌流程?
VIVO手机上del键无效OnKeyListener不响应的原因及解决方法
Laravel如何优化应用性能?(缓存和优化命令)
详解jQuery中基本的动画方法
ChatGPT回答中断怎么办 引导AI继续输出完整内容的方法
微信小程序 HTTPS报错整理常见问题及解决方案
Laravel API资源类怎么用_Laravel API Resource数据转换
PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)
Laravel与Inertia.js怎么结合_使用Laravel和Inertia构建现代单页应用
Laravel广播系统如何实现实时通信_Laravel Reverb与WebSockets实战教程
公司网站制作需要多少钱,找人做公司网站需要多少钱?
Laravel PHP版本要求一览_Laravel各版本环境要求对照
html5如何设置样式_HTML5样式设置方法与CSS应用技巧【教程】
厦门模型网站设计制作公司,厦门航空飞机模型掉色怎么办?
Laravel怎么做缓存_Laravel Cache系统提升应用速度的策略与技巧
C++时间戳转换成日期时间的步骤和示例代码
Laravel怎么上传文件_Laravel图片上传及存储配置
Android中AutoCompleteTextView自动提示
Windows10怎样连接蓝牙设备_Windows10蓝牙连接步骤【教程】
如何在IIS中新建站点并配置端口与IP地址?
昵图网官网入口 昵图网素材平台官方入口
如何用狗爹虚拟主机快速搭建网站?
Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】
Laravel怎么实现软删除SoftDeletes_Laravel模型回收站功能与数据恢复【步骤】
使用Dockerfile构建java web环境
javascript中的try catch异常捕获机制用法分析
如何在IIS中新建站点并配置端口与物理路径?
Win11任务栏卡死怎么办 Windows11任务栏无反应解决方法【教程】
文字头像制作网站推荐软件,醒图能自动配文字吗?
Laravel Docker环境搭建教程_Laravel Sail使用指南
详解MySQL数据库的安装与密码配置
如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?
Laravel如何实现RSS订阅源功能_Laravel动态生成网站XML格式订阅内容【教程】
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
如何在七牛云存储上搭建网站并设置自定义域名?
黑客如何通过漏洞一步步攻陷网站服务器?
Windows10如何删除恢复分区_Win10 Diskpart命令强制删除分区
详解Oracle修改字段类型方法总结
北京网页设计制作网站有哪些,继续教育自动播放怎么设置?
如何确认建站备案号应放置的具体位置?
Laravel如何与Inertia.js和Vue/React构建现代单页应用
三星、SK海力士获美批准:可向中国出口芯片制造设备
个人网站制作流程图片大全,个人网站如何注销?
深圳网站制作培训,深圳哪些招聘网站比较好?
Laravel怎么返回JSON格式数据_Laravel API资源Response响应格式化【技巧】

