Android乐学成语
效果图
具体实现以及写的过程中遇到的问题
第一步:建立数据库,像这种比较繁多的数据,可以用execl表格来做,然后Navict可视化工具,导入进去
加载数据数据库到项目中来,在res目录下建立一个raw文件夹,
DBOpenHelper.java
package
cn.bzu
.bztc.happyidiom.db;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
cn.bzu
.bztc.happyidiom.activity.R;
import
android.content.Context;
import
android.database.sqlite.SQLiteDatabase;
import
android.os.Environment;
public
class
DBOpenHelper {
private
final
int
BuFFWER_SIZE=
400000
;
//缓冲区大小
public
static
final
String DB_NAME=
"idioms.db"
;
//保存的数据库文件名
public
static
final
String PACKAGE_Name=
"cn.deu.bztc.happyidiom.activity"
;
//应用的包名
public
static
final
String DB_PATH=
"/data"
+Environment.getDataDirectory().getAbsolutePath()+
"/"
+PACKAGE_Name+
"/databases"
;
//在手机里存放数据库的位置
private
Context context;
public
DBOpenHelper(Context context) {
super
();
this
.context = context;
}
public
SQLiteDatabase openDatabase(){
try
{
File myDataPath=
new
File(DB_PATH);
if
(!myDataPath.exists()){
myDataPath.mkdirs();
//如果没有这个目录则创建
}
String dbfile=myDataPath+
"/"
+DB_NAME;
if
(!(
new
File(dbfile).exists())){
//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
InputStream is=context.getResources().openRawResource(R.raw.idioms);
FileOutputStream fos=
new
FileOutputStream(dbfile);
byte
[] buffer=
new
byte
[BuFFWER_SIZE];
int
count=
0
;
while
((count=is.read(buffer))>
0
){
fos.write(buffer,
0
, count);
}
fos.close();
is.close();
}
SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(dbfile,
null
);
return
db;
}
catch
(Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return
null
;
}
}
上面的代码实现功能主要是使用输入输出流将idioms.db复制到手机中默认存放 数据库的位置
在test包下建立DBOpenHelpTest.java(链接测试)
package
cn.deu.bztc.happyidiom.test;
import
java.util.List;
import
cn.bzu
.bztc.happyidiom.dao.AnimalDao;
import
cn.bzu
.bztc.happyidiom.db.DBOpenHelper;
import
cn.bzu.bztc.happyidiom.entity.Animal;
import
android.test.AndroidTestCase;
public
class
DBOpenHelpTest
extends
AndroidTestCase {
public
void
testDBCOpy(){
DBOpenHelper dbopenHelper=
new
DBOpenHelper(getContext());
dbopenHelper.openDatabase();
}
public
void
testGetAllAnimals(){
AnimalDao animalDao=AnimalDao.getInstance(getContext());
List animals=animalDao.getAllAnimals();
System.out.println(animals.size());
for
(Animal animal:animals){
System.out.println(animal.getName());
}
}
}
![](https://images2015.cnblogs.com/blog/905382/201606/905382-20160615131856370-842362897.png)
然后你会发现在raw目录下多个数据库
happyidiom.entity建立实体类
public
class
Animal {
private
int
id;
private
String name;
//成语名称
private
String pronounce;
//成语发音
private
String explain;
//成语解释
private
String antonym;
//反义词
private
String homoionym;
//同义词
private
String derivation;
//源自
private
String examples;
//例子
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getPronounce() {
return
pronounce;
}
public
void
setPronounce(String pronounce) {
this
.pronounce = pronounce;
}
public
String getExplain() {
return
explain;
}
public
void
setExplain(String explain) {
this
.explain = explain;
}
public
String getAntonym() {
return
antonym;
}
public
void
setAntonym(String antonym) {
this
.antonym = antonym;
}
public
String getHomoionym() {
return
homoionym;
}
public
void
setHomoionym(String homoionym) {
this
.homoionym = homoionym;
}
public
String getDerivation() {
return
derivation;
}
public
void
setDerivation(String derivation) {
this
.derivation = derivation;
}
public
String getExamples() {
return
examples;
}
public
void
setExamples(String examples) {
this
.examples = examples;
}
}
然后在数据库管理层 (Dao 层)建立操作类AnimalDao.java
public
class
AnimalDao {
private
static
AnimalDao animalDao;
private
static
SQLiteDatabase db;
/**
* 将构造方法私有化
*/
public
AnimalDao(Context context) {
DBOpenHelper dbHelper=
new
DBOpenHelper(context);
db=dbHelper.openDatabase();
}
/**
* 获取AnimalDao实例
*/
public
synchronized
static
AnimalDao getInstance(Context context){
if
(animalDao==
null
){
animalDao=
new
AnimalDao(context);
}
return
animalDao;
}
/**
* 从数据库读取所有的动物类成语
*/
public
static
List getAllAnimals(){
List list=
new
ArrayList();
Cursor cursor=db.query(
"animal"
,
null
,
null
,
null
,
null
,
null
,
null
);
if
(cursor.moveToFirst()){
do
{
Animal animal=
new
Animal();
animal.setId(cursor.getInt(cursor.getColumnIndex(
"_id"
)));
animal.setName(cursor.getString(cursor.getColumnIndex(
"name"
)));
animal.setPronounce(cursor.getString(cursor.getColumnIndex(
"pronounce"
)));
animal.setAntonym(cursor.getString(cursor.getColumnIndex(
"antonym"
)));
animal.setHomoionym(cursor.getString(cursor.getColumnIndex(
"homoionym"
)));
animal.setDerivation(cursor.getString(cursor.getColumnIndex(
"derivation"
)));
animal.setExamples(cursor.getString(cursor.getColumnIndex(
"examples"
)));
list.add(animal);
}
while
(cursor.moveToNext());
}
return
list;
}
}
下面进行单元测试,如下
![](https://images2015.cnblogs.com/blog/905382/201606/905382-20160615132402104-1580913999.png)
UI界面
![](https://images2015.cnblogs.com/blog/905382/201606/905382-20160615132442698-1722627065.png)
然后在res的values目录的string.xml文件中定义
<string-array name=
"category"
>
<item>动物类</item>
<item>自然类</item>
<item>人物类</item>
<item>季节类</item>
<item>数字类</item>
<item>寓言类</item>
<item>其他类</item>
</string-array>
happyidiom.activity中建立MainActivity.java
public
class
MainActivity
extends
TabActivity {
private
TabHost tabHost;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//取消标题栏
setContentView(R.layout.activity_main);
tabHost=getTabHost();
addTab(
"study"
, R.string.title_study, R.drawable.study,StudyActivity.
class
);
addTab(
"search"
, R.string.title_search, R.drawable.search, StudyActivity.
class
);
addTab(
"study"
, R.string.title_game, R.drawable.game, StudyActivity.
class
);
addTab(
"save"
, R.string.title_save, R.drawable.save, StudyActivity.
class
);
addTab(
"help"
, R.string.title_help, R.drawable.search, StudyActivity.
class
);
}
private
void
addTab(String tag,
int
title_introduction,
int
title_icon,Class ActivityClass){
tabHost.addTab(tabHost.newTabSpec(tag)
.setIndicator(getString(title_introduction),
getResources().getDrawable(title_icon)).setContent(
new
Intent(
this
,ActivityClass)));
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
}
在这个类里调用TabHost组件, 然后调用了抽取出来的自定义的方法addTob()添加了五个选项卡,方法的四个参数分别为每个选项卡的tag,指示器上显示的标题。,指示器上显示的图片,选项卡对应的内容。