其实显示所有动物类成语的列表和显示学习列表的实现差不多,无非就是自定义适配器的使用,就不再进行详细论述。
1.显示所有动物类成语列表的操作实现后,需要修改StudyActivity中的点击事件。
<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>
此时的界面
2.此事点击收藏按钮没有任何反应,我们来处理一下点击事件。
<span style="font-size:18px;">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);
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);
}else{
view=convertView;
viewHolder=(ViewHolder) view.getTag();
}
viewHolder.tvName.setText(animal.getName());
return view;
}
class ViewHolder{
TextView tvName;
ImageButton btnSave;
}
}</span>
然后重新运行一下程序,此时的界面为