乐学成语

     学了这么久的android开发,是时候该考验一下自己了!
         做项目之前·,我么首先做一下需求分析。如何做到完整呢?我们需要做到以下几个方面。

一:成语分类学习

二:乐猜成语

三:成语收藏

四:成语查询

        可不能小看这四个简单的功能,但是做起来却不是那么简单,需要用到UI、网络、数据存储等技术。要想得到所有的成语数据,获取每个成语的读音、解释、近义词、反义词等信息,很幸运,聚合网给我们提供了免费的聚合数据。此网址为: 点击打开链接
        这就是我们的最终效果图 大笑
         
        首先我们需要创建happyidiom这个项目,在Eclipse中新建一个Android项目,叫做HappyIdiom,包名叫做cn.edu.bztc.happyidiom.使用4.4的API。
1.1创建数据库
      我们需要把已有的数据库传入/data/data/package name/目录下,思路是FileInputStream读取原数据库,再用FileOutputStream把读取到的东西写入到这个目录,这样就可以方便的操作数据库了。首先,在res目录下新建raw目录,将数据库复制到此目录下。
      在db包下新建一个DBOpenHelper类,代码如下所示:
     
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 android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import cn.edu.bztc.happyidiom.R;

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.happyidiom";
	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("Database", "File not found");
			e.printStackTrace();
		} catch (IOException e) {
			Log.e("Dtaabase", "IO exception");
			e.printStackTrace();
		}
		return null;

	}
}
     然后搭建单元测试环境,测试数据库有没有创建到指定的路径下面。首先需要修改AndroidManfest.xml文件搭建起单元测试环境。修改的文件如下:
     
   <uses-library android:name="android.test.runner"/>
    </application>
<instrumentation 
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="cn.edu.bztc.happyidiom">
    
</instrumentation>
</manifest>

        只要加上上述代码,单元测试环境就搭建起来了了。
        接下来在test包下,新建DBOpenHelperTest继承AndroidTestCase。代码如下:
       
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();
	}

}
          该方法调用了DBOpenHelper类里面定义的openDatabase()方法。Run as后选择Android JUnit Test进行单元测试,成功为绿色,不成功为红色的条。
    
     切换到DDMS,我们发现在/data/data/应用的包下成功的创建了数据库。
    

    另外,entity包下新建一个Animal实体类,代码如下:
       
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;
	}

}
   然后创建一个AnimalDao类,这个类会把一些常用的数据库操作封装起来,以方便我们后来使用,代码如下:
package cn.edu.bztc.happyidiom.dao;

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

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




public class AnimalDao {
     private static AnimalDao animalDao;
     private SQLiteDatabase db;
     private AnimalDao(Context context){
    	 DBOpenHelper dbHelper=new DBOpenHelper(context);
    	 db=dbHelper.openDatabase();
    	 
     }
     public synchronized static AnimalDao getInstance(Context context){
		if(animalDao==null){
			animalDao=new AnimalDao(context);
			
		}
		return animalDao;
    	 
     }
     public  List<Animal> getAllAnimals(){
    	 List<Animal> list =new ArrayList<Animal>();
    	 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.setExampless(cursor.getString(cursor.getColumnIndex("examples")));
    			 list.add(animal);
    		 }while(cursor.moveToNext());
    	 }
    	
     
     return list;
    	 
     }
   
}

   将AnimalDao的构造方法私有化,并提供了一个getInstance()方法来获取AnimalDao的实例,接下来我们在AnimalDao中提供了一个方法getAllAnimals()该方法用来获取所有的动物类成语。代码如下:
package cn.edu.bztc.happyidiom.test;

import java.util.List;

import cn.edu.bztc.happyidiom.dao.AnimalDao;
import cn.edu.bztc.happyidiom.db.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());
		}
	}

}
     好了,第一阶段的代码到这里就差不多了。
1.2 显示主界面
       第二阶段我们完成主界面的设计。
             在res的drawable-hdpi目录下拷贝需要的图片素材,在res/layout目录下新建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" >

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

                <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>

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

</RelativeLayout>
   布局文件中的内容比较简单,主要是拖了一个TabHost控件到界面上。

     然后再res的values目录下的strings.xml文件中定义所需的字符串。代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">HappyIdiom</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_main">MainActivity</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-array name="category">
    <item>动物类</item>
    <item>自然类</item>
    <item>人物类</item>
    <item>季节类</item>
    <item>数字类</item>
    <item>寓言类</item>
    <item>其他类</item>
 </string-array>
</resources>
  接下来需要在avtivity包下新建一个MainActivity继承自Activity,代码如下:
package cn.edu.bztc.happyidiom.activity;


import cn.edu.bztc.happyidiom.R;
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();
		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);
	}
	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 preaent.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
		
	}

}


在这个类的oncreate()方法里,通过调用getTabHost()方法来获取整个TabHost组件,然后调用了抽取出来的自定义的方法addTab()添加了五个选项卡,方法的四个参数分别为每个选项卡的tag,指示器上显示的标题,指示器上显示的图片,选项卡对应的内容。注意取消标题栏的方法,一定要位于setContextView()方法之前。然后在配置AndroidManfest.xml文件。代码如下:
    
 <activity
            android:name="cn.edu.bztc.happyidiom.activity.MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

  运行之后,只有文字,没有图片,要想图片和文字一块显示,需要设置activity的theme为
    android:theme="@android:style/Theme.Black.NoTitleBar" >
图片如下:
</pre></div><div style="text-align:left"><span style="font-size:10px"><img src="" alt="" /></span></div><div style="text-align:left"><span style="font-size:10px"></span></div><div style="text-align:left"><span style="font-size:10px">要想让其在指示器的底部显示,需要修改以下操作:</span><pre name="code" class="html">  <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
   到此为止,本阶段任务基本完成。
1.3 显示学习列表
  第三阶段,我们要以列表的形式显示出成语的分类。现在就对listview的界面进行定制。首先需要准备好一组图片,分别对应每一种类别,我们要让这些类别的名称的旁边都有一个图样。准备好文字资源,修改values中的string.xml文件,代码在上述文件中有所显示。
       接着定义一个实体类,作为ListView适配器的适配类型,在entity包下新建类Category,代码如下所示:
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;
     }
}
在Layout下新建activity_study.xml文件,添加一个ListView:
<?xml version="1.0" encoding="utf-8"?>
<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/IvCategories"
        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>

在layout目录下新建category_item.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: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="@array/category"
     android:gravity="center"
     android:textAppearance="?android:attr/textAppearance"  />
   
</LinearLayout>

新建CategoryAdapter继承自ArrayAdapter,代码如下所示:
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) {
			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.setFocusable(false);
			viewHolder.btnSave.setFocusableInTouchMode(false);

			viewHolder.btnSave.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					Toast.makeText(context, "你要收藏" + animal.getName() + "吗",
							Toast.LENGTH_SHORT).show();

				}
			});

			view.setTag(viewHolder);//将ViewHolder存储在View中
		} else {
			view = convertView;
			viewHolder = (ViewHolder) view.getTag();//重新获取ViewHolder
		}
		viewHolder.tvName.setText(animal.getName());

		return view;

	}

	class ViewHolder {
		TextView tvName;
		ImageButton btnSave;
	}

}


下面在activity包下新建StudyActivity继承自Activity,代码如下所示:
public class StudyActivity extends Activity {
	private List<Category> categoryList;
	private String[] category_names;
	private int[] category_images;

	@Override
	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setContentView(R.layout.activity_study);
		initCategories();// 初始化类别
		CategoryAdapter adapter = new CategoryAdapter(this,
				R.layout.category_item, categoryList);
		ListView listView = (ListView) findViewById(R.id.IvCategories);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				// Category category=categoryList.get(position);
				// Toast.makeText(StudyActivity.this, category.getName(),
				// Toast.LENGTH_LONG).show();
				switch (position) {
				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.study, menu);
		return true;
	}

}
可以看到,这里添加了一个initCategory()方法,用于初始化所有类别数据,在运行之前,首先在需要修改AndroidManifest.xml文件将StudyActivity变为入口类。
运行结果如图所示:

   修改CategoryAdapter中的代码,在上述代码中有提到。在此不做详细介绍。
   通过我们的优化后,我们的ListView的运行效率很不错了。
    ListView的滚动毕竟只是给了我们视觉上的效果,但是如果ListView的子项不能点击的话,这个控件就没有什么用途了,修改StudyActivity中的代码在上述代码中有提到,就不在列举。可以看到我们使用了setOnItemCilckListener()方法来为Listview注册了一个监听器,当用户点击了ListView中任何一个子项时,就会回调onitemClick()方法,在这个方法中可以通过position参数判断出用户点击的是哪一个子项,然后获取到相应的类别,并通过Toast将类别的名字显示出来。运行程序后的结果为:

  界面载入的过程,我们为其增加淡入淡出的动画效果。在res目录下新建anim目录,在下面新建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>
然会在study_activity.xml中修改此代码:
 android:layoutAnimation="@anim/anim_layout_listview"
现在我们的任务是将其与之前建立的界面连接起来。代码如下所示:
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("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);
	}
	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 preaent.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
		
	}

}

1.4 显示所有动物类成语的列表

  将数据库中所有动物类的成语显示在主界面上。

    在layout下新建activity_animal.xml文件,主要添加了一个ListView控件。代码如下所示:

<span style="font-size:10px;"><?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/IvAnimalList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutAnimation="@anim/anim_layout_listview"
        android:listSelector="#00000000" >
    </ListView>

</LinearLayout></span>
在layout下新建animal_item.xml,代码如下:

<span style="font-size:10px;"><?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:padding="10dp" >

    <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:text="助人为乐"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

</RelativeLayout>
</span>

在这个布局中,我们定义了一个TextView用于显示成语的名称,又定义了一个ImageButton用于显示收藏按钮,新建AnimalAdapter继承自ArrayAdapter,代码在上述中有所提到,不做解释。

在activity包下新建StudyAnimalActivity继承自Activity,代码如下:

<span style="font-size:10px;">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.IvAnimalList);
		AnimalAdapter animalAdapter = new AnimalAdapter(this,R.layout.animal_item, animalList);
		lvAnimalList.setAdapter(animalAdapter);

		lvAnimalList.setOnItemClickListener(new OnItemClickListener() {
			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.getExampless();
				DialogUtil.showDialog(result, StudyAnimalActivity.this);
			}
		
		});
	}

	private void initAnimals() {
		animalDao = AnimalDao.getInstance(this);
		animalList = animalDao.getAllAnimals();
	}

}
</span>

程序运行结果如图所示:

<span style="font-size:10px;">
</span>

修改AnimalAdapter类,加入事件处理。代码在上述中提到,不做解释。

本阶段任务基本完成。

1.5显示每条成语的详细信息

    本阶段的任务是点击成语义对话框的形式显示。
   代码如下:
<?xml version="1.0" encoding="utf-8"?>
<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:background="@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>
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 tvIdiomInfo = (TextView) view.findViewById(R.id.tvIdiomInfo);
		tvIdiomInfo.setText(result);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
}
运行结果图为:



最后修改AndroidManifest,.xml中的代码:
android:icon=“@drowable/logo”
如图:
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值