andriod之乐学成语

来源:互联网 发布:嵌入式c笔试 编程题 编辑:程序博客网 时间:2024/04/29 17:19
效果图大笑








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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.deu.bztc.happyidiom.db;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStream;  
  6.   
  7. import cn.deu.bztc.happyidiom.activity.R;  
  8. import android.content.Context;  
  9. import android.database.sqlite.SQLiteDatabase;  
  10. import android.os.Environment;  
  11.   
  12. public class DBOpenHelper {  
  13.     private final int BuFFWER_SIZE=400000;//缓冲区大小  
  14.     public static final String DB_NAME="idioms.db";//保存的数据库文件名  
  15.     public static final String PACKAGE_Name="cn.deu.bztc.happyidiom.activity";  
  16.     //应用的包名  
  17.     public static final String DB_PATH="/data"  
  18.             +Environment.getDataDirectory().getAbsolutePath()+"/"  
  19.             +PACKAGE_Name+"/databases";//在手机里存放数据库的位置  
  20.     private Context context;  
  21.     public DBOpenHelper(Context context) {  
  22.         super();  
  23.         this.context = context;  
  24.     }  
  25.     public SQLiteDatabase openDatabase(){  
  26.         try{  
  27.             File myDataPath=new File(DB_PATH);  
  28.             if(!myDataPath.exists()){  
  29.                 myDataPath.mkdirs(); //如果没有这个目录则创建  
  30.             }  
  31.             String dbfile=myDataPath+"/"+DB_NAME;  
  32.             if(!(new File(dbfile).exists())){  
  33.                 //判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库  
  34.                 InputStream is=context.getResources().openRawResource(R.raw.idioms);  
  35.                 FileOutputStream fos=new FileOutputStream(dbfile);  
  36.                 byte[] buffer=new byte[BuFFWER_SIZE];  
  37.                 int count=0;  
  38.                 while((count=is.read(buffer))>0){  
  39.                     fos.write(buffer, 0, count);  
  40.                 }  
  41.                 fos.close();  
  42.                 is.close();  
  43.             }  
  44.             SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(dbfile, null);  
  45.             return db;  
  46.         }catch (Exception e) {  
  47.             // TODO: handle exception  
  48.             e.printStackTrace();  
  49.         }  
  50.         return null;  
  51.     }  
  52. }  

测试一下是否成功
在AndroidManifest.xml中建立单元测试环境
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cn.deu.bztc.happyidiom.activity"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="17"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/logo"  
  14.         android:label="@string/app_name"  
  15.         android:screenOrientation="portrait"  
  16.         android:theme="@android:style/Theme.Translucent.NoTitleBar" >  
  17.        <span style="color:#ff0000;"> <uses-library android:name="android.test.runner" /></span>  
  18.   
  19.         <activity  
  20.             android:name="cn.deu.bztc.happyidiom.activity.MainActivity"  
  21.             android:label="@string/app_name" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.         <activity  
  29.             android:name="cn.deu.bztc.happyidiom.activity.StudyAnimalActivity"  
  30.             android:label="@string/app_name" >  
  31.         </activity>  
  32.         <activity  
  33.             android:name="cn.deu.bztc.happyidiom.util.DialogUtil"  
  34.             android:label="@string/app_name" >  
  35.         </activity>  
  36.         <activity  
  37.             android:name="cn.deu.bztc.happyidiom.activity.StudyActivity"  
  38.             android:label="@string/app_name" >  
  39.         </activity>  
  40.         <activity  
  41.             android:name="com.tangsci.android.example.ttsdemo.TtsDemoActivity"  
  42.             android:label="@string/app_name" >  
  43.         </activity>  
  44.     </application>  
  45.   
  46.     <span style="color:#ff0000;"><instrumentation  
  47.         android:name="android.test.InstrumentationTestRunner"  
  48.         android:targetPackage="cn.deu.bztc.happyidiom.activity" >  
  49.     </instrumentation></span>  
  50.   
  51. </manifest>  

测试一下是否成功:
在test包下建立DBOpenHelpTest.java
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.deu.bztc.happyidiom.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.deu.bztc.happyidiom.dao.AnimalDao;  
  6. import cn.deu.bztc.happyidiom.db.DBOpenHelper;  
  7. import cn.deu.bztc.happyidiom.entity.Animal;  
  8. import android.test.AndroidTestCase;  
  9.   
  10. public class DBOpenHelpTest extends AndroidTestCase {  
  11.     public void testDBCOpy(){  
  12.         DBOpenHelper  dbopenHelper=new DBOpenHelper(getContext());  
  13.         dbopenHelper.openDatabase();  
  14.     }  
  15.     public void testGetAllAnimals(){  
  16.         AnimalDao animalDao=AnimalDao.getInstance(getContext());  
  17.         List<Animal> animals=animalDao.getAllAnimals();  
  18.         System.out.println(animals.size());  
  19.         for(Animal animal:animals){  
  20.             System.out.println(animal.getName());  
  21.         }  
  22.     }  

cn.deu.bztc.happyidiom.entity建立实体类  
Animal.java
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.deu.bztc.happyidiom.entity;  
  2.   
  3. public class Animal {  
  4.     private int id;    
  5.     private String name;//成语名称  
  6.     private String pronounce;//成语发音  
  7.     private String explain;//成语解释  
  8.     private String antonym;//反义词  
  9.     private String homoionym;//同义词  
  10.     private String derivation;//源自  
  11.     private String examples;//例子  
  12.     public int getId() {  
  13.         return id;  
  14.     }  
  15.     public void setId(int id) {  
  16.         this.id = id;  
  17.     }  
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.     public String getPronounce() {  
  25.         return pronounce;  
  26.     }  
  27.     public void setPronounce(String pronounce) {  
  28.         this.pronounce = pronounce;  
  29.     }  
  30.     public String getExplain() {  
  31.         return explain;  
  32.     }  
  33.     public void setExplain(String explain) {  
  34.         this.explain = explain;  
  35.     }  
  36.     public String getAntonym() {  
  37.         return antonym;  
  38.     }  
  39.     public void setAntonym(String antonym) {  
  40.         this.antonym = antonym;  
  41.     }  
  42.     public String getHomoionym() {  
  43.         return homoionym;  
  44.     }  
  45.     public void setHomoionym(String homoionym) {  
  46.         this.homoionym = homoionym;  
  47.     }  
  48.     public String getDerivation() {  
  49.         return derivation;  
  50.     }  
  51.     public void setDerivation(String derivation) {  
  52.         this.derivation = derivation;  
  53.     }  
  54.     public String getExamples() {  
  55.         return examples;  
  56.     }  
  57.     public void setExamples(String examples) {  
  58.         this.examples = examples;  
  59.     }  
  60.       
  61. }  
然后在数据库管理层 (Dao 层)建立操作类AnimalDao.java大笑
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.deu.bztc.happyidiom.dao;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import cn.deu.bztc.happyidiom.db.DBOpenHelper;  
  7. import cn.deu.bztc.happyidiom.entity.Animal;  
  8. import android.content.Context;  
  9. import android.database.Cursor;  
  10. import android.database.sqlite.SQLiteDatabase;  
  11.   
  12. public class AnimalDao {  
  13.     private static AnimalDao animalDao;  
  14.     private static SQLiteDatabase db;  
  15.     /** 
  16.      * 将构造方法私有化 
  17.      */  
  18.     public AnimalDao(Context  context) {  
  19.         DBOpenHelper dbHelper=new DBOpenHelper(context);  
  20.         db=dbHelper.openDatabase();  
  21.     }  
  22.     /** 
  23.      * 获取AnimalDao实例 
  24.      */  
  25.     public synchronized static AnimalDao getInstance(Context context){  
  26.         if(animalDao==null){  
  27.             animalDao=new AnimalDao(context);  
  28.         }  
  29.         return animalDao;  
  30.     }  
  31.     /** 
  32.      * 从数据库读取所有的动物类成语 
  33.      */   
  34.     public static  List<Animal> getAllAnimals(){  
  35.         List<Animal> list=new ArrayList<Animal>();  
  36.         Cursor cursor=db.query("animal"nullnullnullnull,null,null);  
  37.         if(cursor.moveToFirst()){  
  38.             do{  
  39.                 Animal animal=new Animal();  
  40.                 animal.setId(cursor.getInt(cursor.getColumnIndex("_id")));  
  41.                 animal.setName(cursor.getString(cursor.getColumnIndex("name")));  
  42.                 animal.setPronounce(cursor.getString(cursor.getColumnIndex("pronounce")));  
  43.                 animal.setAntonym(cursor.getString(cursor.getColumnIndex("antonym")));  
  44.                 animal.setHomoionym(cursor.getString(cursor.getColumnIndex("homoionym")));  
  45.                 animal.setDerivation(cursor.getString(cursor.getColumnIndex("derivation")));  
  46.                 animal.setExamples(cursor.getString(cursor.getColumnIndex("examples")));  
  47.                 list.add(animal);  
  48.             }while(cursor.moveToNext());  
  49.         }  
  50.         return list;  
  51.     }  
  52. }  
下面进行单元测试
在test测试类下加入
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public void testGetAllAnimals(){  
  2.         AnimalDao animalDao=AnimalDao.getInstance(getContext());  
  3.         List<Animal> animals=animalDao.getAllAnimals();  
  4.         System.out.println(animals.size());  
  5.         for(Animal animal:animals){  
  6.             System.out.println(animal.getName());  
  7.         }  


测试成功后,实现功能

activity_main.xml
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity"  
  6.     android:background="@drawable/bg_animal" >  
  7.   
  8.     <TabHost  
  9.         android:id="@android:id/tabhost"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true" >  
  14.   
  15.         <LinearLayout  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="match_parent"  
  18.             android:orientation="vertical" >  
  19.   
  20.             <FrameLayout  
  21.                 android:id="@android:id/tabcontent"  
  22.                 android:layout_width="match_parent"  
  23.                 android:layout_height="match_parent"  
  24.                 android:layout_weight="1" >  
  25.   
  26.                 <LinearLayout  
  27.                     android:id="@+id/tab2"  
  28.                     android:layout_width="match_parent"  
  29.                     android:layout_height="match_parent"  
  30.                     android:orientation="vertical" >  
  31.                 </LinearLayout>  
  32.   
  33.                 <LinearLayout  
  34.                     android:id="@+id/tab3"  
  35.                     android:layout_width="match_parent"  
  36.                     android:layout_height="match_parent"  
  37.                     android:orientation="vertical" >  
  38.                 </LinearLayout>  
  39.             </FrameLayout>  
  40.   
  41.             <TabWidget  
  42.                 android:id="@android:id/tabs"  
  43.                 android:layout_width="match_parent"  
  44.                 android:layout_height="wrap_content" >  
  45.             </TabWidget>  
  46.         </LinearLayout>  
  47.     </TabHost>  
  48.   
  49. </RelativeLayout>  

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <string-array name="category">  
  2.        <item>动物类</item>  
  3.        <item>自然类</item>  
  4.        <item>人物类</item>  
  5.        <item>季节类</item>  
  6.        <item>数字类</item>  
  7.        <item>寓言类</item>  
  8.        <item>其他类</item>  
  9.    </string-array>  
接下来cn.deu.bztc.happyidiom.activity中建立MainActivity.java
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.deu.bztc.happyidiom.activity;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.app.TabActivity;  
  6. import android.content.Intent;  
  7. import android.view.Menu;  
  8. import android.view.Window;  
  9. import android.widget.TabHost;  
  10.   
  11. public class MainActivity extends TabActivity {  
  12.     private TabHost  tabHost;  
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏  
  17.         setContentView(R.layout.activity_main);  
  18.         tabHost=getTabHost();  
  19.         addTab("study", R.string.title_study, R.drawable.study,StudyActivity.class);  
  20.         addTab("search", R.string.title_search, R.drawable.search, StudyActivity.class);  
  21.         addTab("study", R.string.title_game, R.drawable.game, StudyActivity.class);  
  22.         addTab("save", R.string.title_save, R.drawable.save, StudyActivity.class);  
  23.         addTab("help", R.string.title_help, R.drawable.search, StudyActivity.class);  
  24.     }  
  25.     private void addTab(String tag,int title_introduction,int title_icon,Class ActivityClass){  
  26.         tabHost.addTab(tabHost.newTabSpec(tag)  
  27.                 .setIndicator(getString(title_introduction),  
  28.                         getResources().getDrawable(title_icon)).setContent(new Intent(this,ActivityClass)));  
  29.     }  
  30.       
  31.     @Override  
  32.     public boolean onCreateOptionsMenu(Menu menu) {  
  33.         // Inflate the menu; this adds items to the action bar if it is present.  
  34.         getMenuInflater().inflate(R.menu.main, menu);  
  35.         return true;  
  36.     }  
  37.   

0 0
原创粉丝点击