上篇完成了显示学习列表和连接主界面,现在我们来完成所有动物类成语的列表和每条成语的详细信息。
首先,显示所有的动物类列表。
在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"
>
<ListView
android: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"
>
<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/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 stub
this.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() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
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。在其中添加了一个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;
@Override
protected 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() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
// TODO Auto-generated method stub
Animal 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 stub
animalDao=AnimalDao.getInstance(this);
animalList=animalDao.getAllAnimals();
}
@Override
public 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;
@Override
protected 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() {
@Override
public 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,StudyAnimalActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
}
private void initCategory() {
// TODO Auto-generated method stub
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) {
// 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变为入口类。
<activity
android: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" >
<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="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() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
}
}
最后运行程序,效果如图:
好了,乐学成语的案例到现在为止就全都完成了,现在欣赏一下这个程序的所有运行效果吧。