上篇完成了显示学习列表和连接主界面,现在我们来完成所有动物类成语的列表和每条成语的详细信息。



首先,显示所有的动物类列表。


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

<strong><span style="font-size:18px;"><strong><span style="font-size:18px;"><?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="vertical" android:background="@drawable/bg_animal"><ListViewandroid:id="@+id/lvAnimalList"android:layout_width="match_parent"android:layout_height="wrap_content" android:listSelector="#00000000"android:layoutAnimation="@anim/anim_layout_listview"  ></ListView>
</LinearLayout></span></strong></span></strong>

为ListView的子项指定一个自定义的布局,在layout目录下新建一个animal_item.xml文件,在这个布局中,我们定义了一个TextView用于显示成语的名称,定义了一个ImageButton用于显示收藏按钮。

<strong><span style="font-size:18px;"><?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/btnStar"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@null"android:layout_alignParentRight="true"android:layout_alignTop="@+id/tvName"android:src="@android:drawable/btn_star" /></RelativeLayout></span></strong>

在应用的包下新建Adapter包,在其中新建AnimalAdapter类继承自ArrayAdapter,并增加点击事件处理和获取焦点组件,代码如下:

package com.edu.happyidiom.adapter;import java.util.List;import com.edu.happyidiom.adapter.CategoryAdapter.ViewHolder;
import com.edu.happyidiom.entity.Animal;
import com.edu.happyidiom.entity.Category;
import com.example.happyidiom.R;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.ImageView;
import android.widget.TextView;
import android.widget.Toast;public class AnimalAdapter extends ArrayAdapter<Animal> {private int resourceld;private Context context;public AnimalAdapter(Context context, int resource,List<Animal> objects) {super(context, resource, objects);// TODO Auto-generated constructor stubthis.context=context;resourceld=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(resourceld, null);viewHolder=new ViewHolder();viewHolder.tvName= (TextView) view.findViewById(R.id.tvName);viewHolder.btnSave= (ImageButton) view.findViewById(R.id.btnStar);viewHolder.btnSave.setFocusable(false);//获取焦点viewHolder.btnSave.setFocusableInTouchMode(false);//获取焦点viewHolder.btnSave.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 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;}}

在Activity包下新建StudyAnimalActivity继承自Activity。在其中添加了一个InitAnimals()方法,用于初始化所有的动物数据。

<span style="font-size:18px;"><strong>package com.example.happyidiom;import java.util.List;import com.edu.happyidiom.adapter.AnimalAdapter;
import com.edu.happyidiom.dao.AnimalDao;
import com.edu.happyidiom.entity.Animal;
import com.edu.happyidiom.util.DialogUtil;import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;public class StudyAnimalActivity extends Activity {private List<Animal> animalList;private AnimalDao animalDao;private ListView lvAnimalList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_study_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.getAutonym()+"\n【来源】:"+animal.getDerivation()+"\n【示例】:"+animal.getExamples();DialogUtil.showDialog(result, StudyAnimalActivity.this);}});}private void initAnimals() {// TODO Auto-generated method stubanimalDao=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.study_animal, menu);return true;}}</strong></span>

修改StudyActivity中的点击事件,代码如下:

<strong><span style="font-size:18px;">package com.example.happyidiom;import java.util.ArrayList;
import java.util.List;import com.edu.happyidiom.adapter.CategoryAdapter;
import com.edu.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);initCategory();//初始化类别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 stubswitch(position){case 0:Intent intent=new Intent(StudyActivity.this,StudyAnimalActivity.class);startActivity(intent);break;default:break;}}});}private void initCategory() {// 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;}}
</span></strong>

最后修改一下AndroidMainifest.xml文件,将StudyActivity变为入口类。

 <activityandroid:name="com.example.happyidiom.StudyActivity"android:label="@string/title_activity_study" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity>

修改后重新运行程序,运行效果如图:



其次,显示每条成语的详细信息。


在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" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_ling"android:orientation="vertical" ><TextViewandroid:id="@+id/tvIdiomInfo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium"/></LinearLayout></ScrollView>

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

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

最后运行程序,效果如图:


好了,乐学成语的案例到现在为止就全都完成了,现在欣赏一下这个程序的所有运行效果吧。

              

                  






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

  1. Linux 命令之 lsusb -- 显示本机的USB设备列表信息

    文章目录 命令介绍 常用选项 命令示例 (一)显示 USB 设备详细信息 命令介绍 lsusb命令用于显示本机的USB设备列表,以及USB设备的详细信息. lsusb命令显示的USB设备信息来自&qu ...

  2. ElementUI中el-table双击单元格事件并获取指定列的值和弹窗显示详细信息

    场景 双击el-table的某个单元格时获取此单元格的信息并弹窗显示其他关联的信息. 效果如下 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公 ...

  3. 【JSP篇】——cookie之商品浏览记录的实现:4.显示商品的详细信息

    学习上一节:3.显示当前所有的商品效果与功能的实现 学习下一节:5.cookie实现前五条浏览记录 1.功能介绍 前面我们实现了商品所有信息的显示,接下来要实现某一商品详细信息的显示.那么我们就要思考 ...

  4. js操作元素:改变元素内容有两种方式: interText 和 innerHTML的区别, 常用的元素的属性操作, 仿电脑系统时间问好 ,表单元素的属性操作 ,案例:仿京东显示隐藏密码 ,样式属性操作

    文章目录 操作元素 改变元素内容有两种方式: interText 和 innerHTML的区别 常用的元素的属性操作 案例:仿电脑系统时间问好 表单元素的属性操作 案例:仿京东显示隐藏密码 样式属性操 ...

  5. 让iis7.5显示php错误的详细信息~

    这两天在win7下调试php程序, 用的是fastcgi模式的,也不知这个模式到底怎么样, 但既然win7和win2008都默认支持这个模式, 那就应该是不错的, 而且php5.3以上版本也在win7 ...

  6. 剥开比原看代码17:比原是如何显示交易的详细信息的?

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...

  7. android 模仿大众点评团购卷列表多余3条时折叠,点击时显示剩余全部的功能

    要实现这样一个效果:加载一组数据,当这组数据的条数超过2条时,则这显示两条,其余的隐藏,当点击"展开全部时"在显示余下的部分.效果如下图所示: 展开前的效果: 展开后的效果 : 实 ...

  8. .Net有关问题。在GridView中添加了一个HyperLinkField,用来显示显示详细信息,请教跳转后的详细信息页面该怎样写代码

    .Net问题...在GridView中添加了一个HyperLinkField,用来显示显示详细信息,请问跳转后的详细信息页面该怎样写代码 在一个页面的GridView中添加了下面的代码: <as ...

  9. 让Win10文件管理器的详细信息窗格显示更多信息

    当你在Windows的资源管理器中将详细信息窗格设置为可用,然后选中一个文件时(笔者选中了一个exe文件),你将在窗口的右半部分看到这个文件的一些信息,包括修改日期.创建日期.文件大小和可用性,不过对 ...

最新文章

  1. 底板芯片组与内存映射(Motherboard Chipsets and the Memory Map) 【转】
  2. 合并报表编制采用的理论_合并报表操作的整体思路梳理
  3. 终极JPA查询和技巧列表–第3部分
  4. Windows更新补丁下载、批量安装的几种方法
  5. 转liunx 常用命令
  6. 你可能不知道的 Android Studio 小技巧之「多行编辑」
  7. PostgreSQL 与 PostGIS
  8. PR免费转场 动态图形转场PR模板MOGRT免费下载
  9. Renascence架构介绍——文件夹
  10. 教师资格证报名网页打不开,解决新版IE浏览器无法打开教师资格证页面问题(不需要添加兼容性站点!)
  11. 2018年列车已到站,请您下车
  12. 33.什么是wildcard(通配符)查询?如何进行通配符查询?如何使用java进行wildcard (通配符)查询?嘻哈的简写笔记——Elastic Search
  13. 内齿轮铣齿机铣削动力头的设计(论文+CAD图纸)
  14. Koomail:企业网管的好助手!
  15. 2019美国大学计算机专业硕士申请条件,104@2019美国大学计算机(CS)硕士申请难度大比拼(二)...
  16. 解决企业数字化“历史遗留问题”,低代码具备天然优势 | 专访腾讯云微搭骆勤
  17. Mondrian利用在Schema中的设置角色来实现行级权限控制数据
  18. CCS 之 关于TI 28035 SPI的一点问题
  19. 2019秋招求职攻略
  20. 新手小白怎么做自媒体?自媒体原创文章怎么写?这3点很重要!

热门文章

  1. 为什么你的广告投放效果不好?这5点做到了吗?
  2. 数据结构---散列表(哈希表)链地址法
  3. NVDIMM编程模型原理大概推导梳理
  4. 紫薇星上的数据结构(10)
  5. Java代码如何运行的?
  6. ArcGis for Android 集成天地图四川(一)
  7. macOS Mojave 10.14.6 18G103 正式版 with Clover 5091原版镜像[双EFI双平台终极版]
  8. 随笔 | 写作的意义
  9. java秒换算成时分秒的形式
  10. 趣头条递交招股书 将冲刺移动内容聚合第一股