一、创建数据库和表

1、创建好数据库和表(数据库名字为:idioms)

2、首先创建一个Android项目,项目名字为HappyIdiom,包名为cn.edu.bztc.happyidiom,然后在此包下再建几个包:

activity包用于存放所有活动相关的代码,db包用于存放所有数据库相关的代码,entity包用于存放所有实体相关的代码,dao包用于存放数据操作相关的代码,util包用于存放所有工具相关的代码。

3、把数据库idioms传入到android的数据库data/data/package name/目录下:

(1)用FileInPutStream读取元数据库

(2)再用FileOutputStream把读取到的东西写入到那个目录

*注意*:首先在res目录下新建raw目录,将idioms.db数据库复制到此目录下。

【因为raw目录的东西,android会原封不动的拷贝到程序中,而不会转换为二进制文件。】

在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 cn.edu.bztc.happyidiom.R;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;/*** 实现将数据库文件从raw目录拷贝到手机里存放数据库的位置* @author YOYO**/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) {// TODO Auto-generated catch blockLog.e("Database", "File not found");e.printStackTrace();}catch(IOException e){Log.e("Database", "IO exception");e.printStackTrace();}        return null;}}

上面的代码实现的主要功能是使用输入输出流将idioms.db复制到手机中默认存放数据库的位置。

有没有复制成功呢?那就要进行单元测试看看,测试数据库有没有创建到指定的路径下面。

(3)首先修改AndroidManifest.xml文件搭建起单元测试的环境。修改后的内容如下:

 <applicationandroid:allowBackup="true"android:icon="@drawable/logo"android:label="@string/app_name"android:theme="@android:style/Theme.Black.NoTitleBar" ><uses-library android:name="android.test.runner" /></application><instrumentationandroid:name="android.test.InstrumentationTestRunner"android:targetPackage="cn.edu.bztc.happyidiom" ></instrumentation>

(4)然后在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();}}

该类只封装了一个方法,测试方法通常命名为testXXX()。该方法调用了DBOpenHelper类里面定义的openDatabase()方法。

在测试中,如果出现如图所示的绿条,则代表测试成功;

如果测试代码中存在错误,则会出现红条并在下方显示出错的原因。

(5)接下来看看数据库有没有复制成功,切换到DDMS,会发现在data/data/应用的包下成功的创建了数据库。

4、Animal表还存在一个对应的实体类,因此,在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;}
}

5、接着还需要创建一个AnimalDao类,这个类将会把一些常用的数据库操作封装起来,以方便后面使用,代码如下:

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 animalDao;private SQLiteDatabase db;/*** 构造方法私有化* @param context*/private AnimalDao(Context context){DBOpenHelper dbHelper=new DBOpenHelper(context);db=dbHelper.openDatabase();}/*** 获取AnimalDao的实例* @param context* @return*/public synchronized static AnimalDao getInstance(Context context){if(animalDao==null){animalDao=new AnimalDao(context);}return animalDao;     }/*** 从数据库读取所有的动物类成语* @return*/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.setExplain(cursor.getString(cursor.getColumnIndex("explain")));animal.setAntonym(cursor.getString(cursor.getColumnIndex("antonym")));animal.setHomoionym(cursor.getString(cursor.getColumnIndex("homoionym")));animal.setDerivation(cursor.getString(cursor.getColumnIndex("derivation")));animal.setExamples(cursor.getString(cursor.getColumnIndex("examples")));list.add(animal);}while(cursor.moveToNext());}return list;     }}

AnimalDao是一个单例类,将它的构造方法私有化,并提供了一个getInstance()方法来获取AnimalDao的实例,这样就可以保证全局范围内只会有一个AnimalDao的实例。还提供了一个getAllAnimals()方法来获取所有的动物类成语。
6、编写单元测试类AnimalDaoTest继承AndroidTestCase,代码如下:

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());}}}

运行单元测试,结果如下图所示:

二、显示主界面

1、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">  <LinearLayoutandroid: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">                </TabWidget>                        <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:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent">                 </LinearLayout><LinearLayout android:id="@+id/tab2"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent">                 </LinearLayout>  <LinearLayout android:id="@+id/tab3"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent">                 </LinearLayout>   </FrameLayout></LinearLayout>      </TabHost></RelativeLayout>

布局文件中的内容比较简单,主要是托了一个TabHost控件到界面上,如图所示:


2、在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><string name="title_activity_study">StudyActivity</string><string name="title_activity_animal">AnimalActivity</string></resources>

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

package cn.edu.bztc.happyidiom.activity;import cn.edu.bztc.happyidiom.R;
import cn.edu.bztc.happyidiom.R.layout;
import cn.edu.bztc.happyidiom.R.menu;
import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;
import android.widget.TabHost;public class MainActivity extends TabActivity {private TabHost tabHost;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);    //取消标题栏 setContentView(R.layout.activity_main);tabHost=getTabHost();//如果没有继承TabActivity时,通过该种方法加载启动tabHost//tabHost.setup();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 TabHost getTabHost() {// TODO Auto-generated method stub//获取TabHost对象TabHost tabHost=(TabHost) findViewById(R.id.tabhost);return tabHost;}*/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)));}   @Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

在这个类的onCreate()方法里,通过调用getTabHost()方法来获取整个TabHost组件,然后调用抽取出来的自定义的方法addTab()添加了五个选项卡。方法的四个参数分别为每个选项卡的tag,指示器上显示的标题,指示器上显示的图片,选项卡对应的内容。(将学习界面与之前建立的主界面连起来)

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

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

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

  

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

如设置activity的theme为:

android:theme="@android:style/Theme.Black.NoTitleBar" 

如果想让指示器显示在底部,只需要对activity_main.xml文件稍加修改,将<TabWidget> </TabWidget>中的代码放在<FrameLayout></FrameLayout>代码之后即可。

三、显示学习列表

1、定义一个实体类,作为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 void setName(String name) {this.name = name;}public int getImageId() {return imageId;}public void setImageId(int imageId) {this.imageId = imageId;}}

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

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

3、需要为ListView的子项指定一个自定义的布局,新建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" ><ImageViewandroid:id="@+id/category_image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/category_animal" /><TextViewandroid: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/textAppearanceLarge" /></LinearLayout>

定义了一个ImageView用于显示类别的图片,定义了一个textView用于显示类别的名称。

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

package cn.edu.bztc.happyidiom.adapter;import java.util.List;import cn.edu.bztc.happyidiom.R;
import cn.edu.bztc.happyidiom.entity.Category;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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> objects) {super(context, resource, objects);// TODO Auto-generated constructor stubresourceId=resource;}public View getView(int position,View convertView,ViewGroup parent){Category category=getItem(position);//获取当前项的Category实例//View view=LayoutInflater.from(getContext()).inflate(resourceId, null);//ImageView cateoryImage=(ImageView) view.findViewById(R.id.category_image);//TextView categoryName=(TextView) view.findViewById(R.id.category_name);//cateoryImage.setImageResource(category.getImageId());//categoryName.setText(category.getName());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);//将ViewHolder存储在View中}else{view = convertView;viewHolder=(ViewHolder) view.getTag();//重新获取ViewHolder}     viewHolder.categoryImage.setImageResource(category.getImageId());viewHolder.categoryName.setText(category.getName());return view;       }class ViewHolder{ImageView categoryImage;TextView categoryName;}}

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,代码如下:

package cn.edu.bztc.happyidiom.activity;import java.util.ArrayList;
import java.util.List;import cn.edu.bztc.happyidiom.R;
import cn.edu.bztc.happyidiom.R.layout;
import cn.edu.bztc.happyidiom.R.menu;
import cn.edu.bztc.happyidiom.adapter.CategoryAdapter;
import cn.edu.bztc.happyidiom.entity.Category;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
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;@Overrideprotected 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() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int position,long id) {// TODO Auto-generated method stub               switch(position){case 0: Intent intent=new Intent(StudyActivity.this,AnimalActivity.class);startActivity(intent);break;default:break;}             }});}private void initCategories() {// TODO Auto-generated method stubcategoryList=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]));}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.study, menu);return true;}}

添加了一个initCategories()方法,用于初始化所有的类别数据。在Fruit类的构造函数中将类别的名字和对应的图片id传入,然后把创建好的对象添加到类别列表中。接着在onCreate()方法中创建了CategoryAdapter对象,并将 CategoryAdapter作为适配器传递给ListView。这样定制ListView界面的任务就完成了。

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

运行程序如图所示:

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

<ListView android:id="@+id/lvCategories"android:layout_width="match_parent"android:layout_height="wrap_content"android:listSelector="#00000000"<span style="white-space:pre">  </span>//去掉橙黄色的背景色android:layoutAnimation="@anim/anim_layout_listview"android:layout_alignParentLeft="true"android:layout_alignParentTop="true">        </ListView>

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

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

设置一个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>

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

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

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".AnimalActivity" ><ListViewandroid:id="@+id/lvAnimalList"android:layout_width="match_parent"android:layout_height="wrap_content"android:layoutAnimation="@anim/anim_layout_listview"android:listSelector="#00000000"></ListView></LinearLayout>

2、需要为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:padding="10dp" ><TextViewandroid: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" /><ImageButtonandroid: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>

在这个布局中,定义了一个TextView用于显示成语的名称,又定义了一个ImageButton用于显示收藏按钮。

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

显示弹窗:

<span style="white-space:pre">  </span>viewHolder.btnSave.setFocusable(false);viewHolder.btnSave.setFocusableInTouchMode(false);
package cn.edu.bztc.happyidiom.adapter;import java.util.List;import cn.edu.bztc.happyidiom.R;
import cn.edu.bztc.happyidiom.adapter.CategoryAdapter.ViewHolder;
import cn.edu.bztc.happyidiom.entity.Animal;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
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);// TODO Auto-generated constructor stubthis.context=context;resourceId=resource;}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() {@Overridepublic void onClick(View view) {// TODO Auto-generated method stubToast.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;}}

4、新建AnimalActivity类,代码如下所示:

package cn.edu.bztc.happyidiom.activity;import java.util.List;import cn.edu.bztc.happyidiom.R;
import cn.edu.bztc.happyidiom.R.layout;
import cn.edu.bztc.happyidiom.R.menu;
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.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;public class AnimalActivity extends Activity {private List<Animal> animalList;private AnimalDao animalDao;private ListView lvAnimalList;@Overrideprotected 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() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int position,long id) {// TODO Auto-generated method stubAnimal 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, AnimalActivity.this);}});}private void initAnimals(){animalDao=AnimalDao.getInstance(this);animalList=animalDao.getAllAnimals();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.animal, menu);return true;}}

添加了一个initAnimals()方法,用于初始化所有的动物数据。然后获取ListView控件,建立AnimalAdapter关联子布局及数据,调用ListView控件的setAdapter()方法与关联数据,这样定制ListView界面的任务就完成了。
DialogUtil.showDialog()方法是自定义的方法。在util包下新建DialogUtil类,如下所示:

package cn.edu.bztc.happyidiom.util;import cn.edu.bztc.happyidiom.R;
import android.app.AlertDialog;
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 tvldiomlnfo=(TextView) view.findViewById(R.id.tvldiomlnfo);tvldiomlnfo.setText(result);builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog.dismiss();}});builder.create().show();}}

运行如图所示:

  

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

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

<?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" ><TextViewandroid:id="@+id/tvldiomlnfo"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Medium Text"android:textAppearance="?android:attr/textAppearanceMedium" /></LinearLayout>
</ScrollView>

最外层是ScrollView组件,当内容较多时会自动出现垂直滚动条。
运行如图所示:

六、修改图标和名称

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

<applicationandroid:allowBackup="true"android:icon="@drawable/logo"<span style="white-space:pre"> </span>//修改图标android:label="@string/app_name"android:theme="@android:style/Theme.Black.NoTitleBar" ><uses-library android:name="android.test.runner" />
</application>

乐学成语(HappyIdiom)相关推荐

  1. 个人完成案例之乐学成语(显示所有动物类成语的列表和每条成语的详细信息)

    上篇完成了显示学习列表和连接主界面,现在我们来完成所有动物类成语的列表和每条成语的详细信息. 首先,显示所有的动物类列表. 在layout下新建一个activity_animal.xml文件,主要添加 ...

  2. 乐学成语实现之一:创建数据库和表

    <span style="font-size:32px;"> 首先在eclipse中新建一个Android项目,项目名叫做HappyIdiom,包名叫做cn.edu.b ...

  3. 乐学成语——数据库创建(导入、打开)

    1.创建数据库 通过Navicat Premium软件新建一个SQLite文件idioms.db,建表animal信息为 id                    int               ...

  4. Android之乐学成语

    <strong>1.1 功能需求及技术可行性分析</strong> 1.   成语分类学习:用户可以根据自己喜欢的分类(动物类.人物类.季节类.自然类.数字类.寓言类.其他类) ...

  5. Android小程序-乐学成语游戏(四)

    目标效果:      游戏页面有几个小游戏,因为时间原因只做了第一个猜成语,是用的选择题方式,十道题以内答对六题算闯关成功. 1.新建GameActivity.java页面和activity_game ...

  6. 乐学成语——第一部分

     1. 先做好基础工作把数据库导入到项目工程中,这是第一步.    (1)我们知道android的数据库存放在/data/data/package name/目录下,所以我们需要做的是把已有的数据库传 ...

  7. 乐学成语——android(一)

    首先我们先创建如下几个包 其中activity包中用于存放所有活动相关的代码,db包中用于存放所有数据库相关的文件的代码,entity包用于存放所有实体相关的代码,dao包中用于存放数据操作相关的代码 ...

  8. Android小程序-乐学成语背景音乐(五)

    目标效果: 第五个帮助页面改为设置页面,添加switch控件,播放背景音乐. 1.创建SetActivity.java页面和activity_set.xml页面,activity_set.xml页面放 ...

  9. Android SQLite之乐学成语项目数据库存储

    一.SQLite是什么?为什么要用SQLite?SQLite有什么特点?(下面小编一 一解答) ①SQLite是一个轻量级的关系型数据库,运算速度快,占用资源少,很适合在移动设备上使用, 不仅支持 标 ...

最新文章

  1. 新生代Eden与两个Survivor区的解释
  2. python 三引号_Python 字符串
  3. html编译器nvu,开源的网页编辑器Nvu
  4. 计算机动画的主要应用领域,简述计算机的主要特点和主要应用领域
  5. 2011年值得注意的5个设计趋势
  6. JobDataMap传递参数_02
  7. IIS Web怪问题: Access is denied due to invalid credentials.
  8. opencv打开的图片应用于nn.Conv2d()(一)(H, W, C)转为 (C, H, W)
  9. python中while。。。。else的用法
  10. 抽象类 VS 接口(1)
  11. 14.性能之巅 洞悉系统、企业与云计算 --- 附录
  12. Atitti.软件的一些理论补充 Atitti.软件的原理原则定律法则补充 目录 1.1. 分布式领域CAP理论, 1 1.2. 关系数据库的ACID模型拥有 高一致性 + 可用性 很难进行分区:
  13. PHP、Java、Python、C、C++ 这几种编程语言都各有什么特点或优点
  14. Vue 3.0 Ref-sugar 提案到底是啥,真的是自寻死路吗?
  15. 数据库事务Transaction)那点事
  16. iOS 手机安装Charles证书失败解决方法
  17. 人工智能和机器学习在医疗领域中的应用
  18. JAVA EXAMPLE
  19. node内存溢出问题 Javascript Heap out of memory
  20. 数据结构魔王语言问题

热门文章

  1. -2岁的产品经理学习日记2020/2/15
  2. 节能与环保杂志节能与环保杂志社节能与环保编辑部2023年第2期目录
  3. matplotlib学习笔记(六)
  4. 无法识别的usb设备 跟这台计算机,无法识别的USB设备:跟这台计算机连接的一个USB设备运行不正常,WINDOWS无法识别...
  5. android 展示pdf文件
  6. github实用的搜索小技巧
  7. html5 圆圈扩散,CSS3地图动态实例代码(圆圈向外扩散)
  8. win10杀毒防护已经关闭了还是删除文件解决方法
  9. canvas之-------水滴扩散特效
  10. Ubuntu服务器鼠标卡死