乐学成语

    首先分析HappyIdiom中应该具备哪些功能。

1. 成语分类学习:用户可以根据自己喜欢的分类(动物类、人物类、季节类、自然类、数学类、预言类、其他类)来进行选择学习。

2. 乐猜成语:学习之余玩玩猜成语的游戏,寓教于乐。

3. 成语收藏:当用户遇到自己想要保存的成语时,点击保存按钮,即保存到收藏页面以方便用户再次查看,对已经掌握的成语,用户可以从收藏夹中删除。

4. 成语查询: 支持首字母查询,如一心一意,可以输入:yxyy。

  一、创建数据库和表

  首先在com.edu.bztc.happyidiom包下在创建几个包


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

首先使用Navicat Premium可视化界面来创建数据库,选择文件菜单->新建->SQList,在弹出的对话框中输入连接名,选择类型喂SQList3。

在res目录下新建raw目录,将idioms.db数据库复制到raw目录下。


在db包下新建一个DBOpenHelper类,代码如下:

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("Database", "IO exception");
				e.printStackTrace();
			}
		return null;
		
		}				
}
上面代码实现的功能主要是使用输入输出流将idioms.db复制到手机中默认存放数据库的位置。

验证是否复制成功,修改AndroidManifest.xml文件搭建单元测试环境,修改后的文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.bztc.happyidiom"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
     <strong> <span style="background-color: rgb(255, 255, 255);">  <uses-library android:name="android.test.runner"/></span></strong>
        <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
            android:name="cn.edu.bztc.happyidiom.activity.StudyActivity"
            android:label="@string/title_activity_main" >
        </activity>
        <activity
            android:name="cn.edu.bztc.happyidiom.activity.StudyAnimalActivity"
            android:label="@string/title_activity_main" >
        </activity>
       
    </application>
<strong><instrumentation 
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="cn.edu.bztc.happyidiom">
    
</instrumentation>
</strong></manifest>
接下来在test包下新建DBOpenHelperTest继承AndroidTestCase,代码如下
public class DBOpenHelperTest extends AndroidTestCase{
	public void testDBCopy(){
		DBOpenHelper dbOpenHelper = new DBOpenHelper(getContext());
		dbOpenHelper.openDatabase();
	}
}
另外,Animal表还存在一个对应的实体类,因此,在entity包下新建一个Animal类,代码如下:
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 exampless;//例子
	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 getExampless() {
		return exampless;
	}
	public void setExampless(String exampless) {
		this.exampless = exampless;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	
}
接着创建一个AnimalDao类, 这个类将会把一些常用的数据库操作封装起来,以方便后面使用,代码如下:
public class AnimalDao {
	private static AnimalDao animalDao;
	private SQLiteDatabase db;
//将构造方法私有化
	private AnimalDao(Context context){
		DBOpenHelper dbHelper = new DBOpenHelper(context);
		db = dbHelper.openDatabase();
	}

//	获取AnimalDao的实例
	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;
		
	}
}
编写单元测试类AnimalDaoTest继承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());
		}
	}

}
运行测试单元。

二、显示主界面

在res的drawable-hdpi目录下拷入需要的图片素材,在res/layout目录中新建activity_main.xml布局,

布局文件内容比较简单,主要是拖一个TabHost控件到页面,代码如下:

<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>
如图所示:


然后在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">乐学成语</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>

</resources>
接下来,需要编写活动,在activity包下新建MainActivity继承自Activity,代码如下:
@SuppressWarnings("deprecation")
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;
		
	}

}
在此,取消标题栏的方法,一定要位于setContentView()方法之前。

在运行之前,修改AndroidManifest.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>
主要是注册了MainActivity,通过加入 <intent-filter> 将其设置为首先启动的类,运行程序。

运行结果指示器上只显示出了文字,但是图片未显示出来,可以通过修改主题来实现,如设置activity的theme为

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

结果如图所示:


如果想让指示器显示在底部,只需要对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"
              <span style="background-color: rgb(255, 204, 204);"> <strong> android:layout_weight="1"</strong></span> >

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

          <strong style="background-color: rgb(255, 204, 204);">  <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget></strong>
        </LinearLayout>
    </TabHost>

</RelativeLayout>
结果如图所示:


三、显示学习列表

用ListView的界面进行定制以列表的形式显示出成语分类列表,修改values中的strings.xml文件如下:

 <string-array name="category">
        <item>动物类</item>
        <item>自然类</item>
        <item>人物类</item>
        <item>季节类</item>
        <item>数字类</item>
        <item>寓言类</item>
        <item>其他类</item>
    </string-array>
接着定义一个实体类,作为ListView适配器的适配类型,在entity包下新建Category类,代码如下:

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:layoutAnimation="@anim/anim_layout_listview"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
        
    </ListView>
    

</RelativeLayout>
然后需要为ListView的子项指定一个自定义的布局,在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:orientation="horizontal"
    android:padding="10dp" >

    <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:gravity="center"
        android:text="@array/category"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
接下来,需要下应用包线创建adapter包,新建适配器CategoryAdapter类继承自ArrayAdapter,并将泛型定义为Category类,代码如下:

public class CategoryAdapter extends ArrayAdapter<Category> {
	private int resourceId;

	public CategoryAdapter(Context context, int resource, List<Category> objects) {
		super(context, resource, objects);
		resourceId = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Category category = getItem(position);// 获取当前项的Category实例
		// View view=LayoutInflater.from(getContext()).inflate(resourceId,
		// null);

		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
		}

//		ImageView caImageImage = (ImageView) view
//				.findViewById(R.id.category_image);
//		TextView categoryName = (TextView) view
//				.findViewById(R.id.category_name);
//		categoryImage.setImageResource(category.getImageId());
//		categoryName.setText(category.getName());

		viewHolder.categoryImage.setImageResource(category.getImageId());
		viewHolder.categoryName.setText(category.getName());

		return view;

	}

	class ViewHolder {
		ImageView categoryImage;
		TextView categoryName;
	}

}
下面在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;
	}

}

           在运行之前,首先修改AndroidManifest.xml文件将StudyActivity变为入口类,现在重新运行程序。

虽然目前我们定制的页面比较简单,但是只要修改Category_item.xml文件中的内容,就可以使界面复杂化。

                修改CategoryAdapter中的代码,使其在getView()方法中不需要每次都将布局重新加载一遍,优化代码如下:

public class CategoryAdapter extends ArrayAdapter<Category> {
	private int resourceId;

	public CategoryAdapter(Context context, int resource, List<Category> objects) {
		super(context, resource, objects);
		resourceId = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Category category = getItem(position);// 获取当前项的Category实例
		// View view=LayoutInflater.from(getContext()).inflate(resourceId,
		// null);

		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
		}

//		ImageView caImageImage = (ImageView) view
//				.findViewById(R.id.category_image);
//		TextView categoryName = (TextView) view
//				.findViewById(R.id.category_name);
//		categoryImage.setImageResource(category.getImageId());
//		categoryName.setText(category.getName());

		viewHolder.categoryImage.setImageResource(category.getImageId());
		viewHolder.categoryName.setText(category.getName());

		return view;

	}

	class ViewHolder {
		ImageView categoryImage;
		TextView categoryName;
	}

}

当用户点击了ListView中的任何一个子项,然后获取到相应的类别,并通过Toast将类别的名字显示出来。

其次,修改MainActivity代码,将其与之前建立的主界面连接起来。
如图所示


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

接下来,应该将数据库中的所有的动物类成语显示在界面上,在layout下新建Activity_animal.xml文件,主要添加了一个ListView控件,代码如下

<?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>
在layout目录下新建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" >

    <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>
创建一个自定义的适配器,新建类AnimalAdapter,代码如下

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包下新建StudyAnimalActivity继承自Activity,代码如下

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

}
接下来修改StudyActivity中的点击事件

此时点击收藏按钮没有任何反应,修改AnimalAdapter类,加入事件处理,代码如下
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;
	}

}
点击一下收藏按钮,就会显示提示是否收藏当前的成语,如图所示


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

首先在Layout下新建布局文件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" >
        
        
        <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>
最外层的ScrollView组件,当内容较多时会自动出现垂直滚动条。

在util包下新建DialogUtil类,代码如下:

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

编辑完成后,修改图标和名称,最后如图所示:





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值