天天看點

Android小程式-樂學成語學習(一)

目标效果:

Android小程式-樂學成語學習(一)

1.程式所需素材:點選打開連結

2.建立項目,在res目錄下建立raw檔案夾,将素材中idioms.db資料庫複制到該檔案夾下,這是因為raw檔案夾中的東西,android會原封不動的拷貝到程式中,而不會轉換為二進制檔案。

3.src目錄下建立七個包,adapter包用于存放擴充卡,activity包用于存放頁面活動相關的代碼,dao包用于存放資料操作相關的代碼,db包用于資料庫相關的代碼,util包用于存放所有工具相關的代碼,test包用于存放單元測試類,entity用于存放實體類。

4.首先在strings.xml頁面定義所需要的文本資訊。

strings.xml頁面:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">happyidiom</string>
    <string name="action_settings">Settings</string>

    <string name="title_study">學習</string>
    <string name="title_search">搜搜</string>
    <string name="title_game">遊戲</string>
    <string name="title_save">收藏</string>
    <string name="title_help">幫助</string>
    <string name="animal">動物類</string>
    
    <string-array name="category">
        <item>動物類</item>
        <item>自然類</item>
        <item>人物類</item>
        <item>季節類</item>
        <item>數字類</item>
        <item>寓言類</item>
        <item>其它類</item>
    </string-array>
</resources>
           

5.在res目錄下建立anim檔案夾,存放兩個動畫頁面,作為ListView内容顯示時的動畫效果。 anim_listview.xml頁面:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="0.0"
    android:toAlpha="1.0">
</alpha>
           

anim_layout_listview.xml頁面:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/anim_listview"
    android:animationOrder="random"
    android:delay="0.2">
</layoutAnimation>
           

6.layout檔案夾下建立category_item.xml頁面,作為顯示成語類别ListView的子項目。 category_item.xml頁面:

<?xml version="1.0" encoding="utf-8"?>
<!-- “學習”中ListView的子布局 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="horizontal" >
    
    <ImageView
        android:id="@+id/category_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/category_animal"/>

    <TextView
        android:id="@+id/category_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/animal"
        android:gravity="center"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
           

7.建立animal_item.xml頁面,作為顯示詳細成語ListView的子項目。 animal_item.xml頁面:

<?xml version="1.0" encoding="utf-8"?>
<!-- 成語清單布局 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:padding="10dp" >
    <!-- android:descendantFocusability="blocksDescendants"使ListView中的子控件ImageButton失去焦點 -->
    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:textColor="#000000"
        android:text="助人為樂"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <ImageButton
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/tvName"
        android:src="@drawable/btnsave"/>
</RelativeLayout>
           

8.建立dialog_info.xl頁面,用于顯示點選成語顯示成語解釋等資訊的對話框頁面。 dialog_info.xml頁面:

<!-- 對話框布局 -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="@drawable/bg_ling"
        android:orientation="vertical">
        <TextView 
            android:id="@+id/tvIdiomInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    </LinearLayout>
</ScrollView>
           

9.建立android頁面StudyActivity.java頁面和activity_study.xml頁面,activity_study.xml頁面存放成語類别的ListView。 activity_study.xml頁面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_ling"
    tools:context=".StudyActivity" >

    <ListView
        android:id="@+id/lvCategories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="#00000000"
        android:layoutAnimation="@anim/anim_layout_listview"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>
           

10.建立android頁面StudyAnimalActivity.java頁面和activity_animal.xml頁面,activity_animal.xml頁面存放具體成語的ListView。 activity_anmal.xml頁面:

<?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:background="@drawable/bg_animal"
    android:orientation="vertical" >
    <ListView 
        android:id="@+id/lvAnimalList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutAnimation="@anim/anim_layout_listview"
        android:listSelector="#00000000">
        
    </ListView>

</LinearLayout>
           

11.activity_main.xml頁面放置TabHost控件,用于标題的選擇。 activity_main.xml頁面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>
           

12.entity包下建立Category.java類,作為成語類别的實體類。 Category.java頁面:

package cn.edu.bztc.happyidiom.entity;

public class Category {
	private String name;
	private int imageId;
	public Category(String name, int imageId) {
		super();
		this.name = name;
		this.imageId = imageId;
	}
	public String getName() {
		return name;
	}
	public int getImageId() {
		return imageId;
	}
}
           

13.entity包下建立Animal.java類,作為成語具體資訊的實體類。 Animal.java頁面:

package cn.edu.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;
	}
	
}
           

14.db包下建立DBOpenHelper.java頁面,用于将複制到raw檔案夾下的資料庫檔案讀取并儲存到databases檔案夾中。 DBOpenHelper.java頁面:

package cn.edu.bztc.happyidiom.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import cn.edu.bztc.happyidion.activity.R;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

/*實作将資料庫檔案從raw目錄拷貝到手機裡存放資料庫的位置*/
public class DBOpenHelper {
	private final int BUFFER_SIZE=400000;//緩沖區大小
	public static final String DB_NAME="idioms.db";//儲存的資料庫檔案名
	public static final String PACKAGE_NAME="cn.edu.bztc.happyidion.activity";//應用的包名
	public static final String DB_PATH="/data"+Environment.getDataDirectory().getAbsolutePath()+"/"+PACKAGE_NAME+"/databases";//在手機裡存放資料庫的位置
	private Context context;
	public DBOpenHelper(Context context){
		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[BUFFER_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 (FileNotFoundException e) {
			Log.e("MainActivity","File not found");
			e.printStackTrace();
		}catch (IOException e) {
			Log.e("MainActivity","IO exception");
			e.printStackTrace();
		}
		return null;
	}
}
           

15.test包下建立DBOpenHelperTest.java頁面測試一下資料庫是否處理成功。 DBOpenHelperTest.java頁面:

package cn.edu.bztc.happyidiom.test;

import cn.edu.bztc.happyidiom.db.DBOpenHelper;
import android.test.AndroidTestCase;

public class DBOpenHelperTest extends AndroidTestCase{
	public void testDBCopy(){
		DBOpenHelper dbOpenHelper=new DBOpenHelper(getContext());
		dbOpenHelper.openDatabase();
	}
}
           

16.單元測試需要在AndroidManifest.xml頁面進行配置。 </application>上方添加:

<uses-library android:name="android.test.runner"/>
           

</manifest>上方添加:

<instrumentation android:name="android.test.InstrumentationTestRunner"
	    android:targetPackage="cn.edu.bztc.happyidion.activity"></instrumentation>
           

17.測試成功繼續編寫頁面。dao包下建立AnimalDao.java類,擷取資料庫中資料。 AnimalDao.java頁面:

package cn.edu.bztc.happyidiom.dao;

import java.util.ArrayList;
import java.util.List;

import cn.edu.bztc.happyidiom.db.DBOpenHelper;
import cn.edu.bztc.happyidiom.entity.Animal;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AnimalDao {
	private static AnimalDao animaiDao;
	private SQLiteDatabase db;
	/*将構造方法私有化*/
	private AnimalDao(Context context){
		DBOpenHelper dbHelper=new DBOpenHelper(context);
		db=dbHelper.openDatabase();
	}
	/*擷取AnimalDao的執行個體*/
	public synchronized static AnimalDao getInstance(Context context){
		if(animaiDao==null){
			animaiDao=new AnimalDao(context);
		}
		return animaiDao;
	}
	/*從資料庫讀取所有的動物類成語*/
	public List<Animal> getAllAnimals(){
		List<Animal> list=new ArrayList<Animal>();
		Cursor cursor=db.query("animal",null,null,null,null,null,null);
		if(cursor.moveToNext()){
			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")));
				animal.setExplain(cursor.getString(cursor.getColumnIndex("explain")));
				list.add(animal);
			}while(cursor.moveToNext());
		}
		return list;
	}
}
           

18.再次在test包下建立AnimalDaoTest.java測試類,測試是否能夠讀取資料。 AnimalDaoTest.java頁面:

package cn.edu.bztc.happyidiom.test;

import java.util.List;

import cn.edu.bztc.happyidiom.dao.AnimalDao;
import cn.edu.bztc.happyidiom.entity.Animal;
import android.test.AndroidTestCase;

public class AnimalDaoTest extends AndroidTestCase{
	public void testGetAllAnimals(){
		AnimalDao animalDao=AnimalDao.getInstance(getContext());
		List<Animal> animals=animalDao.getAllAnimals();
		System.out.println(animals.size());
		for(Animal animal:animals){
			System.out.println(animal.getName());
		}
	}
}
           

19.adapter包下建立CategoryAdapter.java頁面,處理學習頁面的ListView和學習類别資料源。 CategoryAdapter.java頁面:

package cn.edu.bztc.happyidiom.adapter;

import java.util.List;

import cn.edu.bztc.happyidiom.entity.Category;
import cn.edu.bztc.happyidion.activity.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryAdapter extends ArrayAdapter<Category>{

	private int resourceId;
	public CategoryAdapter(Context context, int resource,
			List<Category> categoryList) {
		super(context,resource, categoryList);
		resourceId=resource;
	}
	public View getView(int position, android.view.View convertView, android.view.ViewGroup parent) {
		Category category=getItem(position);//擷取目前項的Category執行個體
		View view;
		ViewHolder viewHolder;
		if(convertView==null){
			view=LayoutInflater.from(getContext()).inflate(resourceId,null);
			viewHolder=new ViewHolder();
			viewHolder.categoryImage=(ImageView)view.findViewById(R.id.category_image);
			viewHolder.categoryName=(TextView)view.findViewById(R.id.category_name);
			view.setTag(viewHolder);	
		}else{
			view=convertView;
			viewHolder=(ViewHolder) view.getTag();
		}
		viewHolder.categoryImage.setImageResource(category.getImageId());
		viewHolder.categoryName.setText(category.getName());
		return view;
	}
	class ViewHolder{
		ImageView categoryImage;
		TextView categoryName;
	}
}
           

20.adapter包下建立AnimalAdapter.java頁面,處理動物類别頁面的ListView和具體成語資料源。 AnimalAdapter.java頁面:

package cn.edu.bztc.happyidiom.adapter;

import java.util.List;

import cn.edu.bztc.happyidiom.entity.Animal;
import cn.edu.bztc.happyidion.activity.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class AnimalAdapter extends ArrayAdapter<Animal>{

	private int resourceId;
	private Context context;
	public AnimalAdapter(Context context, int resource,List<Animal> objects) {
		super(context,resource, objects);
		this.context=context;
		resourceId=resource;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Animal animal =getItem(position);//擷取目前項的Animal執行個體
		View view;
		ViewHolder viewHolder;
		if(convertView==null){//判斷是否第一次運作,如果是則進入,并将上下文環境儲存進convertView
			view=LayoutInflater.from(getContext()).inflate(resourceId,null);
			viewHolder=new ViewHolder();
			viewHolder.tvName=(TextView)view.findViewById(R.id.tvName);
			viewHolder.btnSave=(ImageButton)view.findViewById(R.id.btnSave);
			viewHolder.btnSave.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Toast.makeText(context,"你要收藏"+animal.getName()+"嗎",Toast.LENGTH_SHORT).show();
				}
			});
			view.setTag(viewHolder);
		}else{//如果不是第一次運作,convertView不為空,直接取出指派給view
			view=convertView;
			viewHolder=(ViewHolder) view.getTag();
		}
		viewHolder.tvName.setText(animal.getName());//顯示成語
		return view;
	}
	class ViewHolder{
		TextView tvName;
		ImageButton btnSave;
	}
}
           

21.在util包下建立DialogUtil.java.java頁面,作為彈出對話框的處理頁面。 DialogUtil.jav頁面:

package cn.edu.bztc.happyidiom.util;

import cn.edu.bztc.happyidion.activity.R;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class DialogUtil {
	public static void showDialog(String result,Context context){
		AlertDialog.Builder builder=new AlertDialog.Builder(context);
		LayoutInflater layoutInflater=LayoutInflater.from(context);
		View view=layoutInflater.inflate(R.layout.dialog_info,null);
		builder.setView(view);
		TextView tvdiomInfo=(TextView)view.findViewById(R.id.tvIdiomInfo);
		tvdiomInfo.setText(result);//設定之前定義的提示語句
		builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();//對話框關閉
			}
		});
		builder.create().show();//打開對話框
	}
}
           

22.MainActivity.java頁面用于配置TabHost控件子頁面。 MainActivity.java頁面:

package cn.edu.bztc.happyidion.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
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();     //擷取TabHost執行個體
        /*一個Tab對應一個name名稱,一個标題,一個圖示,yigeActivity頁面*/
        addTab("study",R.string.title_study,R.drawable.study,StudyActivity.class);
        addTab("search",R.string.title_search,R.drawable.search,StudyActivity.class);
        addTab("game",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.help,StudyActivity.class);
    }
    /*定義每個Tab的顯示内容*/
    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) {
    	getMenuInflater().inflate(R.menu.main, menu);
    	return true;
    }
}
           

23.StudyActivity.java頁面作為學習頁面顯示成語類别。 StudyActivity.java頁面:

package cn.edu.bztc.happyidion.activity;

import java.util.ArrayList;
import java.util.List;


import cn.edu.bztc.happyidiom.adapter.CategoryAdapter;
import cn.edu.bztc.happyidiom.entity.Category;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class StudyActivity extends Activity {
	private List<Category> categoryList;
	private String[] category_names;
	private int[] category_images;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_study);
		initCategories();
		CategoryAdapter adapter=new CategoryAdapter(this,R.layout.category_item,categoryList);
		ListView listView=(ListView) findViewById(R.id.lvCategories);
		listView.setAdapter(adapter);//綁定擴充卡
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				switch (position) {//position為0代表動物類
				case 0:
					Intent intent=new Intent(StudyActivity.this,StudyAnimalActivity.class);
					startActivity(intent);
					break;
				default:
					break;
				}
			}
		});
	}

	private void initCategories() {
		categoryList = new ArrayList<Category>();
		Resources resources = getResources();
		/*擷取資料源*/
		category_names = resources.getStringArray(R.array.category);
		category_images = new int[] { R.drawable.category_animal,
				R.drawable.category_nature, R.drawable.category_human,
				R.drawable.category_season, R.drawable.category_number,
				R.drawable.category_fable, R.drawable.category_other };
		for(int i=0;i<category_names.length;i++){
			categoryList.add(new Category(category_names[i],category_images[i]));
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main,menu);
		return true;
	}
}
           

24.StudyAnimalActivity.java頁面用于顯示動物類别中具體的成語資訊,以及每一項的點選事件。 StudynimalActivity.java頁面:

package cn.edu.bztc.happyidion.activity;

import java.util.List;

import cn.edu.bztc.happyidiom.adapter.AnimalAdapter;
import cn.edu.bztc.happyidiom.dao.AnimalDao;
import cn.edu.bztc.happyidiom.entity.Animal;
import cn.edu.bztc.happyidiom.util.DialogUtil;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class StudyAnimalActivity extends Activity{
	private List<Animal> animalList;
	private AnimalDao animalDao;
	private ListView lvAnimalList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_animal);
		initAnimals();
		lvAnimalList=(ListView)findViewById(R.id.lvAnimalList);
		AnimalAdapter animalAdapter=new AnimalAdapter(this,R.layout.animal_item,animalList);
		lvAnimalList.setAdapter(animalAdapter);
		lvAnimalList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long id) {
				Animal animal=animalList.get(position);
				/*定義對話框中提示語句*/
				String result=animal.getName()+"\n"+
				animal.getPronounce()+
				"\n【解釋】:"+animal.getExplain()+
				"\n【近義詞】:"+animal.getHomoionym()+
				"\n【反義詞】:"+animal.getAntonym()+
				"\n【來源】:"+animal.getDerivation()+
				"\n【示例】:"+animal.getExamples();
				DialogUtil.showDialog(result,StudyAnimalActivity.this);
			}
		});
	}
	/*擷取成語資料*/
	private void initAnimals() {
		animalDao=AnimalDao.getInstance(this);
		animalList=animalDao.getAllAnimals();
	}
}
           

25.到此為止,目錄結構如下。

Android小程式-樂學成語學習(一)

26.源碼: 點選打開連結