乐学成语(HappyIdiom)

来源:互联网 发布:俩字网络新兴词语 编辑:程序博客网 时间:2024/04/29 18:52
[html] view plain copy
  1. package cn.edu.bztc.happyidiom.test;  
  2.   
  3.   
  4.   
  5. import java.util.List;  
  6.   
  7. import cn.edu.bztc.happyidiom.dao.AnimalDao;  
  8. import cn.edu.bztc.happyidiom.entity.Animal;  
  9. import android.test.AndroidTestCase;  
  10.   
  11. public class AnimalDaoTest extends AndroidTestCase{  
  12.     public void testGetAllAnimals(){  
  13.         AnimalDao animalDao=AnimalDao.getInstance(getContext());          
  14.         List<Animal> animals=animalDao.getAllAnimals();  
  15.         System.out.println(animals.size());  
  16.         for(Animal animal:animals){  
  17.             System.out.println(animal.getName());  
  18.         }  
  19.     }  
  20.   
  21. }  
运行单元测试,结果如下图所示:



二、显示主界面

1、activity_main.xml布局,代码如下:

[html] view plain copy
  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.   
  7.     <TabHost   
  8.         android:id="@android:id/tabhost"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true">    
  13.         <LinearLayout  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="match_parent"  
  16.             android:orientation="vertical" >  
  17.             <TabWidget   
  18.                 android:id="@android:id/tabs"  
  19.                 android:layout_width="match_parent"  
  20.                 android:layout_height="wrap_content">                  
  21.             </TabWidget>                          
  22.             <FrameLayout   
  23.                 android:id="@android:id/tabcontent"  
  24.                 android:layout_width="match_parent"  
  25.                 android:layout_height="match_parent"  
  26.                 android:layout_weight="1">  
  27.              <LinearLayout   
  28.                  android:id="@+id/tab1"  
  29.                  android:orientation="vertical"  
  30.                  android:layout_width="match_parent"  
  31.                  android:layout_height="match_parent">                   
  32.              </LinearLayout>  
  33.              <LinearLayout   
  34.                  android:id="@+id/tab2"  
  35.                  android:orientation="vertical"  
  36.                  android:layout_width="match_parent"  
  37.                  android:layout_height="match_parent">                   
  38.              </LinearLayout>    
  39.              <LinearLayout   
  40.                  android:id="@+id/tab3"  
  41.                  android:orientation="vertical"  
  42.                  android:layout_width="match_parent"  
  43.                  android:layout_height="match_parent">                   
  44.              </LinearLayout>     
  45.             </FrameLayout>  
  46.               
  47.         </LinearLayout>        
  48.     </TabHost>  
  49.   
  50. </RelativeLayout>  
布局文件中的内容比较简单,主要是托了一个TabHost控件到界面上,如图所示:


2、在res的values目录的strings.xml文件中定义所需的字符串。代码如下所示:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">HappyIdiom</string>  
  5.     <string name="action_settings">Settings</string>  
  6.     <string name="hello_world">Hello world!</string>  
  7.     <string name="title_activity_main">MainActivity</string>  
  8.     <string name="title_study">学习</string>  
  9.     <string name="title_search">搜搜</string>  
  10.     <string name="title_game">游戏</string>  
  11.     <string name="title_save">收藏</string>  
  12.     <string name="title_help">帮助</string>  
  13.   
  14.     <string-array name="category">  
  15.         <item>动物类</item>  
  16.         <item>自然类</item>  
  17.         <item>人物类</item>  
  18.         <item>季节类</item>  
  19.         <item>数字类</item>  
  20.         <item>寓言类</item>  
  21.         <item>其他类</item>  
  22.     </string-array>  
  23.   
  24.     <string name="title_activity_study">StudyActivity</string>  
  25.     <string name="title_activity_animal">AnimalActivity</string>  
  26.   
  27. </resources>  

3、编写一个活动MainActivity,代码如下:

[html] view plain copy
  1. package cn.edu.bztc.happyidiom.activity;  
  2.   
  3. import cn.edu.bztc.happyidiom.R;  
  4. import cn.edu.bztc.happyidiom.R.layout;  
  5. import cn.edu.bztc.happyidiom.R.menu;  
  6. import android.os.Bundle;  
  7. import android.app.TabActivity;  
  8. import android.content.Intent;  
  9. import android.view.Menu;  
  10. import android.view.Window;  
  11. import android.widget.TabHost;  
  12.   
  13.   
  14. public class MainActivity extends TabActivity {  
  15.     private TabHost tabHost;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         requestWindowFeature(Window.FEATURE_NO_TITLE);  //取消标题栏   
  21.         setContentView(R.layout.activity_main);  
  22.         tabHost=getTabHost();  
  23.         //如果没有继承TabActivity时,通过该种方法加载启动tabHost  
  24.         //tabHost.setup();  
  25.         addTab("study",R.string.title_study,R.drawable.study,StudyActivity.class);  
  26.         addTab("search",R.string.title_search,R.drawable.search,StudyActivity.class);  
  27.         addTab("game",R.string.title_game,R.drawable.game,StudyActivity.class);  
  28.         addTab("save", R.string.title_save, R.drawable.save, StudyActivity.class);  
  29.         addTab("help", R.string.title_help, R.drawable.help, StudyActivity.class);  
  30.     }  
  31.     /*private TabHost getTabHost() {  
  32.         // TODO Auto-generated method stub  
  33.         //获取TabHost对象  
  34.         TabHost tabHost=(TabHost) findViewById(R.id.tabhost);  
  35.         return tabHost;  
  36.           
  37.     }*/  
  38.     private void addTab(String tag,int title_introduction,int title_icon,Class ActivityClass){  
  39.         tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(getString(title_introduction),getResources().getDrawable(title_icon)).setContent(new Intent(this,ActivityClass)));  
  40.     }     
  41.   
  42.     @Override  
  43.     public boolean onCreateOptionsMenu(Menu menu) {  
  44.         // Inflate the menu; this adds items to the action bar if it is present.  
  45.         getMenuInflater().inflate(R.menu.main, menu);  
  46.         return true;  
  47.     }  
  48.   
  49. }  
在这个类的onCreate()方法里,通过调用getTabHost()方法来获取整个TabHost组件,然后调用抽取出来的自定义的方法addTab()添加了五个选项卡。方法的四个参数分别为每个选项卡的tag,指示器上显示的标题,指示器上显示的图片,选项卡对应的内容。(将学习界面与之前建立的主界面连起来)

【注意】:(1)取标题栏的方法,一定要位于setContentView()方法之前。

(2)如果MainActivity继承的是Activity,没有继承TabActivity时,

则要通过tabHost.setup();加载启动tabHost
运行程序如图所示:

  

如果运行结果指示器上只显示出了文字,图片为显示出来,可以通过修改主题来实现。

如设置activity的theme为:

[html] view plain copy
  1. android:theme="@android:style/Theme.Black.NoTitleBar"   
如果想让指示器显示在底部,只需要对activity_main.xml文件稍加修改,将<TabWidget></TabWidget>中的代码放在<FrameLayout></FrameLayout>代码之后即可。


三、显示学习列表

1、定义一个实体类,作为ListView适配器的适配类型。

在entity包下新建类Category,代码如下所示:

[html] view plain copy
  1. package cn.edu.bztc.happyidiom.entity;  
  2.   
  3. public class Category {  
  4.     private String name;//类别名称  
  5.     private int imageId;//类别对应的图片  
  6.     public Category(String name, int imageId) {  
  7.         super();  
  8.         this.name = name;  
  9.         this.imageId = imageId;  
  10.     }  
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.     public void setName(String name) {  
  15.         this.name = name;  
  16.     }  
  17.     public int getImageId() {  
  18.         return imageId;  
  19.     }  
  20.     public void setImageId(int imageId) {  
  21.         this.imageId = imageId;  
  22.     }  
  23.       
  24.   
  25. }  
2、新建activity_study.xml文件,主要添加一个ListView控件,代码如下所示:

[html] view plain copy
  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.     android:background="@drawable/bg_ling"  
  6.     tools:context=".StudyActivity" >  
  7.   
  8.     <ListView   
  9.         android:id="@+id/lvCategories"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:listSelector="#00000000"  
  13.         android:layoutAnimation="@anim/anim_layout_listview"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_alignParentTop="true">          
  16.     </ListView>  
  17.       
  18. </RelativeLayout>  
3、需要为ListView的子项指定一个自定义的布局,新建category_item.xml,代码如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:padding="10dp"  
  6.     android:orientation="horizontal" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/category_image"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:src="@drawable/category_animal" />  
  13.   
  14.     <TextView  
  15.         android:id="@+id/category_name"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="@array/category"  
  19.         android:gravity="center"  
  20.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  21.   
  22. </LinearLayout>  
定义了一个ImageView用于显示类别的图片,定义了一个textView用于显示类别的名称。

4、在adapter包下创建一个自定义的适配器,新建类CategoryAdapter,代码如下:

[html] view plain copy
  1. package cn.edu.bztc.happyidiom.adapter;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.edu.bztc.happyidiom.R;  
  6. import cn.edu.bztc.happyidiom.entity.Category;  
  7. import android.content.Context;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.ArrayAdapter;  
  12. import android.widget.ImageView;  
  13. import android.widget.TextView;  
  14.   
  15. public class CategoryAdapter extends ArrayAdapter<Category>{  
  16.     private int resourceId;  
  17.   
  18.     public CategoryAdapter(Context context, int resource, List<Category> objects) {  
  19.         super(context, resource, objects);  
  20.         // TODO Auto-generated constructor stub  
  21.         resourceId=resource;  
  22.     }  
  23.       
  24.     public View getView(int position,View convertView,ViewGroup parent){  
  25.         Category category=getItem(position);//获取当前项的Category实例  
  26.         //View view=LayoutInflater.from(getContext()).inflate(resourceId, null);  
  27.         //ImageView cateoryImage=(ImageView) view.findViewById(R.id.category_image);  
  28.         //TextView categoryName=(TextView) view.findViewById(R.id.category_name);  
  29.         //cateoryImage.setImageResource(category.getImageId());  
  30.         //categoryName.setText(category.getName());  
  31.         View view;        
  32.         ViewHolder viewHolder;  
  33.         if(convertView==null){  
  34.             view=LayoutInflater.from(getContext()).inflate(resourceId, null);  
  35.             viewHolder=new ViewHolder();  
  36.             viewHolder.categoryImage=(ImageView) view.findViewById(R.id.category_image);  
  37.             viewHolder.categoryName=(TextView) view.findViewById(R.id.category_name);  
  38.             view.setTag(viewHolder);//将ViewHolder存储在View中  
  39.         }else{  
  40.             view = convertView;  
  41.             viewHolder=(ViewHolder) view.getTag();//重新获取ViewHolder  
  42.         }         
  43.         viewHolder.categoryImage.setImageResource(category.getImageId());  
  44.         viewHolder.categoryName.setText(category.getName());  
  45.         return view;          
  46.     }  
  47.     class ViewHolder{  
  48.         ImageView categoryImage;  
  49.         TextView categoryName;  
  50.     }  
  51.   
  52. }  
CategoryAdapter重写了父类的一组构造函数,用于将上下文、ListView子项布局的id和数据都传递进来。另外又重写了getView()方法,这个方法在每个子项被滚动到屏幕内的时候会被调用。在getView()方法中,首先通过getItem()方法得到当前项的Category实例,仔细观察,getView()方法中还有一个convertView参数,这个参数用于将之前加载好的布局进行缓存,以便之后可以进行重用,我们在getView()方法中进行了判断,如果convertView为空,则使用LayoutInflater去加载布局,如果不为空则直接对convertView进行重用,这样就大大提高了ListView的运行效率,在快速滚动的时候也可以表现出更好的性能。并分别调用它们的setImageResource()和setText()方法来设置显示的图片和文字,最后将布局返回,这样自定义的适配器就完成了。

新增了一个内部类ViewHolder,用于对控件的实例进行缓存。当convertView为空时,创建一个ViewHolder对象,并将控件的实例都存放在ViewHolder里,然后调用View的setTag()方法,将ViewHolder对象存储在View中。当convertView不为空时则调用View的setTag()方法,把ViewHolder重新取出。这样所有控件的实例都缓存在了ViewHolder里,就美哟必要每次都通过findViewById()方法来获取控件示例了。
5、在activity包下新建StudyActivity,代码如下:

[html] view plain copy
  1. package cn.edu.bztc.happyidiom.activity;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import cn.edu.bztc.happyidiom.R;  
  7. import cn.edu.bztc.happyidiom.R.layout;  
  8. import cn.edu.bztc.happyidiom.R.menu;  
  9. import cn.edu.bztc.happyidiom.adapter.CategoryAdapter;  
  10. import cn.edu.bztc.happyidiom.entity.Category;  
  11. import android.os.Bundle;  
  12. import android.app.Activity;  
  13. import android.content.Intent;  
  14. import android.content.res.Resources;  
  15. import android.view.Menu;  
  16. import android.view.View;  
  17. import android.widget.AdapterView;  
  18. import android.widget.AdapterView.OnItemClickListener;  
  19. import android.widget.ListView;  
  20. import android.widget.Toast;  
  21.   
  22. public class StudyActivity extends Activity {  
  23.     private List<Category> categoryList;  
  24.     private String[] category_names;  
  25.     private int[] category_images;  
  26.   
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_study);  
  31.           
  32.         initCategories();//初始化类别  
  33.         CategoryAdapter adapter=new CategoryAdapter(this, R.layout.category_item, categoryList);  
  34.         ListView listView=(ListView) findViewById(R.id.lvCategories);  
  35.         listView.setAdapter(adapter);  
  36.         listView.setOnItemClickListener(new OnItemClickListener() {  
  37.   
  38.             @Override  
  39.             public void onItemClick(AdapterView<?> adapterView, View view, int position,  
  40.                     long id) {  
  41.                 // TODO Auto-generated method stub                
  42.                 switch(position){  
  43.                 case 0:   
  44.                     Intent intent=new Intent(StudyActivity.this,AnimalActivity.class);  
  45.                     startActivity(intent);  
  46.                 break;  
  47.                 default:  
  48.                     break;  
  49.                 }                 
  50.             }  
  51.         });  
  52.     }  
  53.   
  54.     private void initCategories() {  
  55.         // TODO Auto-generated method stub  
  56.         categoryList=new ArrayList<Category>();  
  57.         Resources resources=getResources();  
  58.         category_names=resources.getStringArray(R.array.category);  
  59.         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};  
  60.         for(int i=0;i<category_names.length;i++){  
  61.             categoryList.add(new Category(category_names[i],category_images[i]));  
  62.         }  
  63.     }  
  64.   
  65.     @Override  
  66.     public boolean onCreateOptionsMenu(Menu menu) {  
  67.         // Inflate the menu; this adds items to the action bar if it is present.  
  68.         getMenuInflater().inflate(R.menu.study, menu);  
  69.         return true;  
  70.     }  
  71.   
  72. }  
添加了一个initCategories()方法,用于初始化所有的类别数据。在Fruit类的构造函数中将类别的名字和对应的图片id传入,然后把创建好的对象添加到类别列表中。接着在onCreate()方法中创建了CategoryAdapter对象,并将CategoryAdapter作为适配器传递给ListView。这样定制ListView界面的任务就完成了。

使用了setOnItemClickListener()方法来为ListView注册了一个监听器,当用户点击了ListView中的任何一个子项时就会回调onItemClick()方法,在这个方法中可以通过position参数判断出用户点击的是哪一个子项,然后获取到相应的类别,并通过Toast将类别的名字显示出来。

运行程序如图所示:

  
在点击每一项时会出现橙黄色的背景色,如果去点背景色,则在activity_study.xml文件中加一句话:

[html] view plain copy
  1. <ListView   
  2.         android:id="@+id/lvCategories"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:listSelector="#00000000"<span style="white-space:pre">    </span>//去掉橙黄色的背景色  
  6.         android:layoutAnimation="@anim/anim_layout_listview"  
  7.         android:layout_alignParentLeft="true"  
  8.         android:layout_alignParentTop="true">          
  9.     </ListView>  

6、界面载入的过程有点僵硬,为界面增加淡入淡出的动画效果。

在res目录下新建anim目录,在下面创建anim_listview.xml文件,代码如下所示:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="1000"  
  4.     android:fromAlpha="0.0"  
  5.     android:toAlpha="1.0">  
  6.       
  7. </alpha>  
设置一个Alpha动画,从无到有的过程。

创建anim_layout_listview.xml文件,代码如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"      
  3.     android:animation="@anim/anim_listview"  
  4.     android:animationOrder="random"  
  5.     android:delay="0.2">      
  6. </layoutAnimation>  

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

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

[html] view plain copy
  1. <LinearLayout 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.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".AnimalActivity" >  
  10.   
  11.     <ListView  
  12.         android:id="@+id/lvAnimalList"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layoutAnimation="@anim/anim_layout_listview"  
  16.         android:listSelector="#00000000">  
  17.     </ListView>  
  18.   
  19. </LinearLayout>  
2、需要为ListView的子项指定一个自定义的布局,新建animal_item.xml,代码如下所示:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:padding="10dp" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tvName"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:gravity="center"  
  14.         android:text="助人为乐"  
  15.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  16.   
  17.     <ImageButton  
  18.         android:id="@+id/btnSave"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:background="@null"  
  22.         android:layout_alignParentRight="true"  
  23.         android:layout_alignTop="@+id/tvName"  
  24.         android:src="@drawable/btnsave" />  
  25.   
  26. </RelativeLayout>  
在这个布局中,定义了一个TextView用于显示成语的名称,又定义了一个ImageButton用于显示收藏按钮。

3、新建类AnimalAdapter,代码如下所示:

显示弹窗:

[html] view plain copy
  1. <span style="white-space:pre">    </span>viewHolder.btnSave.setFocusable(false);  
  2.     viewHolder.btnSave.setFocusableInTouchMode(false);  


[html] view plain copy
  1. package cn.edu.bztc.happyidiom.adapter;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.edu.bztc.happyidiom.R;  
  6. import cn.edu.bztc.happyidiom.adapter.CategoryAdapter.ViewHolder;  
  7. import cn.edu.bztc.happyidiom.entity.Animal;  
  8. import android.content.Context;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.view.ViewGroup;  
  13. import android.widget.ArrayAdapter;  
  14. import android.widget.ImageButton;  
  15. import android.widget.TextView;  
  16. import android.widget.Toast;  
  17.   
  18. public class AnimalAdapter extends ArrayAdapter<Animal>{  
  19.     private int resourceId;  
  20.     private Context context;  
  21.     public AnimalAdapter(Context context, int resource, List<Animal> objects) {  
  22.         super(context, resource, objects);  
  23.         // TODO Auto-generated constructor stub  
  24.         this.context=context;  
  25.         resourceId=resource;  
  26.     }  
  27.     public View getView(int position,View convertView,ViewGroup parent){  
  28.         final Animal animal=getItem(position);//获取当前项的Animal实例  
  29.         View view;  
  30.         ViewHolder viewHolder;  
  31.         if(convertView==null){  
  32.             view=LayoutInflater.from(getContext()).inflate(resourceId, null);  
  33.             viewHolder=new ViewHolder();  
  34.             viewHolder.tvName=(TextView) view.findViewById(R.id.tvName);              
  35.             viewHolder.btnSave=(ImageButton) view.findViewById(R.id.btnSave);  
  36.             viewHolder.btnSave.setFocusable(false);  
  37.             viewHolder.btnSave.setFocusableInTouchMode(false);  
  38.             viewHolder.btnSave.setOnClickListener(new OnClickListener() {  
  39.                   
  40.                 @Override  
  41.                 public void onClick(View view) {  
  42.                     // TODO Auto-generated method stub  
  43.                     Toast.makeText(context, "你要收藏"+animal.getName()+"吗", Toast.LENGTH_SHORT).show();  
  44.                 }  
  45.             });  
  46.             view.setTag(viewHolder);//将ViewHolder存储在View中  
  47.         }else{  
  48.             view=convertView;  
  49.             viewHolder=(ViewHolder) view.getTag();//重新获取ViewHolder  
  50.         }  
  51.         viewHolder.tvName.setText(animal.getName());          
  52.         return view;          
  53.     }  
  54.     class ViewHolder{  
  55.         TextView tvName;  
  56.         ImageButton btnSave;  
  57.     }  
  58.   
  59. }  
4、新建AnimalActivity类,代码如下所示:

[html] view plain copy
  1. package cn.edu.bztc.happyidiom.activity;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.edu.bztc.happyidiom.R;  
  6. import cn.edu.bztc.happyidiom.R.layout;  
  7. import cn.edu.bztc.happyidiom.R.menu;  
  8. import cn.edu.bztc.happyidiom.adapter.AnimalAdapter;  
  9. import cn.edu.bztc.happyidiom.dao.AnimalDao;  
  10. import cn.edu.bztc.happyidiom.entity.Animal;  
  11. import cn.edu.bztc.happyidiom.util.DialogUtil;  
  12. import android.os.Bundle;  
  13. import android.app.Activity;  
  14. import android.app.Dialog;  
  15. import android.view.Menu;  
  16. import android.view.View;  
  17. import android.widget.AdapterView;  
  18. import android.widget.AdapterView.OnItemClickListener;  
  19. import android.widget.ListView;  
  20.   
  21. public class AnimalActivity extends Activity {  
  22.     private List<Animal> animalList;  
  23.     private AnimalDao animalDao;  
  24.     private ListView lvAnimalList;  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_animal);  
  30.         initAnimals();  
  31.         lvAnimalList=(ListView) findViewById(R.id.lvAnimalList);  
  32.         AnimalAdapter animalAdapter=new AnimalAdapter(this, R.layout.animal_item, animalList);  
  33.         lvAnimalList.setAdapter(animalAdapter);  
  34.         lvAnimalList.setOnItemClickListener(new OnItemClickListener() {  
  35.   
  36.             @Override  
  37.             public void onItemClick(AdapterView<?> adapterView, View view, int position,  
  38.                     long id) {  
  39.                 // TODO Auto-generated method stub  
  40.                 Animal animal=animalList.get(position);  
  41.                 String result=animal.getName()+"\n"+animal.getPronounce()+"\n【解释】:"+animal.getExplain()+"\n【近义词】:"+animal.getHomoionym()+"\n【反义词】:"+animal.getAntonym()+"\n【来源】:"+animal.getDerivation()+"\n【实例】:"+animal.getExamples();  
  42.                 DialogUtil.showDialog(result, AnimalActivity.this);  
  43.             }  
  44.         });  
  45.     }  
  46.       
  47.     private void initAnimals(){  
  48.         animalDao=AnimalDao.getInstance(this);  
  49.         animalList=animalDao.getAllAnimals();  
  50.     }  
  51.   
  52.     @Override  
  53.     public boolean onCreateOptionsMenu(Menu menu) {  
  54.         // Inflate the menu; this adds items to the action bar if it is present.  
  55.         getMenuInflater().inflate(R.menu.animal, menu);  
  56.         return true;  
  57.     }  
  58.   
  59. }  
添加了一个initAnimals()方法,用于初始化所有的动物数据。然后获取ListView控件,建立AnimalAdapter关联子布局及数据,调用ListView控件的setAdapter()方法与关联数据,这样定制ListView界面的任务就完成了。
DialogUtil.showDialog()方法是自定义的方法。在util包下新建DialogUtil类,如下所示:

[html] view plain copy
  1. package cn.edu.bztc.happyidiom.util;  
  2.   
  3. import cn.edu.bztc.happyidiom.R;  
  4. import android.app.AlertDialog;  
  5. import android.content.Context;  
  6. import android.content.DialogInterface;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.widget.TextView;  
  10.   
  11. public class DialogUtil {  
  12.     public static void showDialog(String result,Context context){  
  13.         AlertDialog.Builder builder=new AlertDialog.Builder(context);  
  14.         LayoutInflater layoutInflater=LayoutInflater.from(context);  
  15.         View view=layoutInflater.inflate(R.layout.dialog_info, null);  
  16.         builder.setView(view);  
  17.         TextView tvldiomlnfo=(TextView) view.findViewById(R.id.tvldiomlnfo);  
  18.         tvldiomlnfo.setText(result);  
  19.         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  20.               
  21.             @Override  
  22.             public void onClick(DialogInterface dialog, int which) {  
  23.                 // TODO Auto-generated method stub  
  24.                 dialog.dismiss();  
  25.             }  
  26.         });  
  27.         builder.create().show();  
  28.     }  
  29.   
  30. }  


运行如图所示:

  

五、显示每条成语的详细信息

1、新建布局文件dialog_info.xml,代码如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.     <LinearLayout   
  6.         android:layout_width="match_parent"  
  7.         android:layout_height="match_parent"  
  8.         android:background="@drawable/bg_ling"  
  9.         android:orientation="vertical" >  
  10.   
  11.         <TextView  
  12.             android:id="@+id/tvldiomlnfo"  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="Medium Text"  
  16.             android:textAppearance="?android:attr/textAppearanceMedium" />  
  17.       
  18.     </LinearLayout>  
  19. </ScrollView>  
最外层是ScrollView组件,当内容较多时会自动出现垂直滚动条。
运行如图所示:


六、修改图标和名称

修改AndroidManifest.xml中的代码,如下所示:

[html] view plain copy
  1. <application  
  2.         android:allowBackup="true"  
  3.         android:icon="@drawable/logo"<span style="white-space:pre">   </span>//修改图标  
  4.         android:label="@string/app_name"  
  5.         android:theme="@android:style/Theme.Black.NoTitleBar" >  
  6.         <uses-library android:name="android.test.runner" />  
[html] view plain copy
  1. </application>  



0 0
原创粉丝点击