乐学成语

下面是效果图

 

 

\\\\

目录工程如下:

\

 

具体实现以及写的过程中遇到的问题

第一步:建立数据库,像这种比较繁多的数据,可以用execl表格来做,然后Navict可视化工具,导入进去

加载数据数据库到项目中来,在res目录下建立一个raw文件夹,

DBOpenHelper.java

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cn.deu.bztc.happyidiom.db;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
 
import cn.deu.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复制到手机中默认存放 数据库的位置

 

下面我们来测试一下是否成功

在AndroidManifest.xml中建立单元测试环境

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!--?xml version= "1.0" encoding= "utf-8" ?-->
<manifest android:versioncode= "1" android:versionname= "1.0" package = "cn.deu.bztc.happyidiom.activity" xmlns:android= "http://schemas.android.com/apk/res/android" >
 
     <uses-sdk android:minsdkversion= "17" android:targetsdkversion= "17" >
 
     
        <span style= "color:#ff0000;" > <uses-library android:name= "android.test.runner" ></uses-library></span>
 
         
             <intent-filter>
                 
 
                 <category android:name= "android.intent.category.LAUNCHER" >
             </category></action></intent-filter>
         </activity>
         
         </activity>
         
         </activity>
         
         </activity>
         
         </activity>
     </application>
 
     <span style= "color:#ff0000;" ><instrumentation android:name= "android.test.InstrumentationTestRunner" android:targetpackage= "cn.deu.bztc.happyidiom.activity" >
     </instrumentation></span>
 
</uses-sdk></manifest>

 

下面我们来测试一下是否成功:

在test包下建立DBOpenHelpTest.java

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package cn.deu.bztc.happyidiom.test;
 
import java.util.List;
 
import cn.deu.bztc.happyidiom.dao.AnimalDao;
import cn.deu.bztc.happyidiom.db.DBOpenHelper;
import cn.deu.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());
         }
     }
}
</animal>
\
然后你会发现在raw目录下多个数据库

 

\
cn.deu.bztc.happyidiom.entity建立实体类

Animal.java

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cn.deu.bztc.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

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cn.deu.bztc.happyidiom.dao;
 
import java.util.ArrayList;
import java.util.List;
 
import cn.deu.bztc.happyidiom.db.DBOpenHelper;
import cn.deu.bztc.happyidiom.entity.Animal;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
 
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;
     }
}
</animal></animal></animal>
下面进行单元测试

 

在test测试类下加入

 

?
1
2
3
4
5
6
7
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());
         }</animal>
测试如下

 

\
测试成功后,实现功能

UI界面

\

activity_main.xml

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<relativelayout android:background= "@drawable/bg_animal" android:layout_height= "match_parent" android:layout_width= "match_parent" tools:context= ".MainActivity" xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" >
 
     <tabhost android:id= "@android:id/tabhost" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android:layout_height= "match_parent" android:layout_width= "match_parent" >
 
         <linearlayout android:layout_height= "match_parent" android:layout_width= "match_parent" android:orientation= "vertical" >
 
             <framelayout android:id= "@android:id/tabcontent" android:layout_height= "match_parent" android:layout_weight= "1" android:layout_width= "match_parent" >
 
                 <linearlayout android:id= "@+id/tab2" android:layout_height= "match_parent" android:layout_width= "match_parent" android:orientation= "vertical" >
                 </linearlayout>
 
                 <linearlayout android:id= "@+id/tab3" android:layout_height= "match_parent" android:layout_width= "match_parent" android:orientation= "vertical" >
                 </linearlayout>
             </framelayout>
 
             <tabwidget android:id= "@android:id/tabs" android:layout_height= "wrap_content" android:layout_width= "match_parent" >
             </tabwidget>
         </linearlayout>
     </tabhost>
 
</relativelayout>
\

 

一. TabHost介绍
TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计;
1. TabHost常用组件
TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡;
TabSpec : 代表了选项卡界面, 添加一个TabSpec即可添加到TabHost中;
-- 创建选项卡 : newTabSpec(String tag), 创建一个选项卡;
-- 添加选项卡 : addTab(tabSpec);

 

TabHost的使用请看http://blog.csdn.net/harvic880925/article/details/17120325/

然后在res的values目录的string.xml文件中定义

 

?
1
2
3
4
5
6
7
8
9
<string-array name= "category" >
        <item>动物类</item>
        <item>自然类</item>
        <item>人物类</item>
        <item>季节类</item>
        <item>数字类</item>
        <item>寓言类</item>
        <item>其他类</item>
    </string-array>
接下来cn.deu.bztc.happyidiom.activity中建立MainActivity.java

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cn.deu.bztc.happyidiom.activity;
 
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;
import android.widget.TabHost;
 
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,指示器上显示的标题。,指示器上显示的图片,选项卡对应的内容。

 

注意取消标题,一定位于setContCiew()方法之前

\



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值