Android+SQLite数据库实现的生词记事本功能实例
作者:ITzhongzi 发布时间:2023-06-18 10:41:35
标签: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程序设计有所帮助。
来源:http://blog.csdn.net/itzhongzi/article/details/52250565
0
投稿
猜你喜欢
- 目标效果: 点击动画按钮之后每张牌各自旋转 散开到屏幕上半部分的任意位置之后回到初始位置 比较像LOL男刀的技能动画 : )1: 创建卡牌对
- AlertDialog可以在当前的界面上显示一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertD
- 本文实例为大家分享了java简单实现斗地主发牌的具体代码,供大家参考,具体内容如下问题:参考斗地主的游戏规则,完成一个发牌的功能(54张牌,
- 背景大家在使用Selenium + Chromedriver爬取网站信息的时候,以为这样就能做到不被网站的反爬虫机制发现。但是实际上很多参数
- 一直使用Eclipse环境开发Android,也尝鲜使用过Android Studio去开发,各种IDE配合Android SDK及SDK原
- 本文实例讲述了C#启动进程的几种常用方法。分享给大家供大家参考。具体如下:1.启动子进程,不等待子进程结束private void simp
- 1、前言当提及如何终止一个线程时,部分读者通常立马想到的方法肯定是stop(),但是stop()方法并不被推荐使用(很多规范中是禁止使用的)
- feign传输List的坑无法直接传输List错误方法1@RequestMapping(value = "/stat/mercha
- IntelliJ IDEA 2021最新激活码超详细教程,成功激活到2099年。每天专业人士测试,保证可以使用才分享给大家的,放心大胆使用哈
- 问题描述:在用fabric集成后编译出现如下错误,Error:Cause: hostname in certificate didn'
- 开发环境win10Android Studio效果用于多级菜单展示,或选择。如 每个省,市,县;如 树木的病虫害;关键代码 @overrid
- 文章来源:互联网在使用struts多模块的,找到一些小技巧和经验,与大家分享一下。 关于多module的配置就不说了,只需要用不
- 最近碰到个需求,是希望在Unity有一个按钮,打开后直接跳转淘宝app,打开商品页面。百度了下没有相关的文章,于是我在此分享下。之前开发游戏
- 背景最近好几个项目在运行过程中客户都提出文件上传大小的限制能否设置的大一些,用户经常需要上传好几个G的资料文件,如图纸,视频等,并且需要在上
- 1.map遍历快速实现边距,文字自适应改变大小Container( // padding: EdgeI
- 前言这里主要简单介绍如何使用Camera+SurfaceView自定义相机拍照,如果是Camera2或者是TextureView的可以前往主
- 在本博客中,可以找到一篇《c#实现输出的字符靠右对齐的示例》它有教大家怎样实现字符串输出进行左齐或者是右对齐。本篇的方法,超简单,是使用st
- 背景:在Android中按照数据保存的方式,可以分为如下几种Content Provider (用的SQLite实现),SQLite,Sha
- yaml语法注解配置文件两种形式application.properties和.yaml第一种语法 key=value第二种key:空格va
- 1 自定义类加载器自定义类加载器的代码很简单,只需要继承ClassLoader类,覆写findClass方法即可其默认实现是会抛出一个异常: