1.(1) 经过前面的设计,我们已经完成了Animal实体类及AnimalDao数据访问类的创建,接下来我们要做的事情是将数据库中所有的动物类成语显示在界面上。在layout下新建activity_animal.xml文件,主要添加了一个ListView控件。
<span style="font-family:KaiTi_GB2312;font-size:18px;"><ListView
android:id="@+id/lvAnimalList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/anim_layout_listview"
android:listSelector="#00000000"
></ListView></span>
(2)然后需要为ListView的子项指定一个我们自定义的布局,在layout目录下新建animal_item.xml.在这个布局中,我们定义了一个TextView用于显示成语的名称,又定义了一个ImageButton用于显示收藏按钮。
<span style="font-family:KaiTi_GB2312;font-size:18px;"> <TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
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:background="@null"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/tvName"
android:src="@drawable/btnsave"/> </span>
(3)接下来需要在应用的包下创建adapter包,在该包下创建一个自定义的适配器,这个适配器继承自ArrayAdapter,并将泛型指定为Animal类,新建类AnimalAdapter。AnimalAdapter的实现过程与CategoryAdapter的实现非常相似。
(4)在activity包下新建StudyActivity继承自Activity,这里添加了一个initAnimals()方法,用于初始化所有的动物数据,然后获取ListView控件,建立AnimalAdapter关联子布局及数据,调用ListView控件的setAdapter()方法与关联数据。
(5)接下来我们修改一下StudyActivity中的点击事件。
<span style="font-family:KaiTi_GB2312;font-size:18px;"> switch(position){
case 0:
Intent intent=new Intent(StudyActivity.this,StudyAnimalActivity.class);
startActivity(intent);
break;
default:
break;
//Category category=categoryList.get(position);
//Toast.makeText(StudyActivity.this, category.getName(),Toast.LENGTH_LONG).show();
}
}</span>
重新运行程序出现这个界面。
(6)此时点击收藏按钮没有任何反应,接下来让我们一起来处理一下点击事件。修改AnimalAdapter类,加入事件处理。
<span style="font-family:KaiTi_GB2312;font-size:18px;">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;
}
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(){
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;
}
}
</span>
2.(1)这一节我们将实现点击每条成语以对话框的形式显示该成语的详细信息。
在layout下新建布局文件dialog_info.xml。可以看出,最外层是ScrollView组件,当内容较多时会自动出现垂直滚动条。
<span style="font-family:KaiTi_GB2312;font-size:18px;"><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></span>
(2)接下来,修改StudyAnimalActivity,增加点击事件处理。这里的DialogUtil.showDialog()方法是自定义的方法。
<span style="font-family:KaiTi_GB2312;font-size:18px;">lvAnimalList.setOnItemClickListener(new OnItemClickListener(){
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.getAnytonym()+
"\n[来源]"+animal.getDerivation()+
"\n[示例]"+animal.getExamples();
DialogUtil.showDialog(result,StudyAnimalActivity.this);
}
});</span>
(3)在util包下新建DialogUtil类。
<span style="font-family:KaiTi_GB2312;font-size:18px;">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() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
}
}</span>
可是运行时点击每个成语并不弹出对话框,通过google搜索发现是因为在列表中如果出现类似按钮这种能够获取焦点的组件时,就会出现无法单击每一列表项的情况。
解决的办法是,修改AnimalAdapter类。
<span style="font-family:KaiTi_GB2312;font-size:18px;"> viewHolder.btnSave.setFocusable(false);
viewHolder.btnSave.setFocusableInTouchMode(false);
viewHolder.btnSave.setOnClickListener(new OnClickListener(){</span>