参考:http://www.cnblogs.com/topcoderliu/archive/2011/05/07/2039862.html (引用其中代码时Android 要改成 android)
package mobi.game.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mobi.game.gameProcess.R;
import mobi.game.tool.view.MainMenuItemView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* 放入gallery中的单个元素的view
* **/
public class MainMenuAdapter extends BaseAdapter{
private Context mContext;//存放容器
private List<Map<String, Object>> listItems; //关卡信息集合
private LayoutInflater listContainer; //视图容器
private boolean[] hasChecked; //记录商品选中状态
private String title[] = {"菜鸟级","高手级","大师级"};
private Integer[] imgeIDs = {R.drawable.beauty1,R.drawable.beauty2,R.drawable.beauty3}; //数据
private String[] score = {"100", "200","300"};
private Integer[] gradeIDs = {R.drawable.score_img,R.drawable.score_img,R.drawable.score_img};
/**
* 适配器
* **/
public MainMenuAdapter(Context context){
mContext = context;
//得到数据
this.listItems = this.getListItems();
listContainer = LayoutInflater.from(context); //创建视图容器并设置上下文
hasChecked = new boolean[getCount()];
}
/**
* 得到个数
* **/
@Override
public int getCount(){
return listItems.size();
}
/**
* 得到位置
* **/
public Object getItem(int position){
return null;
}
/**
* 得到位置
* **/
public long getItemId(int position) {
return position;
}
/**
* 记录勾选关卡
* @param checkedID 选中的关卡序号
*/
private void checkedChange(int checkedID) {
hasChecked[checkedID] = !hasChecked[checkedID];
}
/**
* 判断物品是否选择
* @param checkedID 关卡序号
* @return 返回是否选中状态
*/
public boolean hasChecked(int checkedID) {
return hasChecked[checkedID];
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// final int selectID = position; //设置选中项
MainMenuItemView menuItemView = null; //自定义视图
if (convertView == null) {
menuItemView = new MainMenuItemView();
convertView = listContainer.inflate(R.layout.main_menu_item, null); //获取list_item布局文件的视图
menuItemView.setTitle((TextView)convertView.findViewById(R.id.titleItem));//获取控件对象
menuItemView.setImage((ImageView)convertView.findViewById(R.id.imageItem));
menuItemView.setScore((TextView)convertView.findViewById(R.id.main_score));
menuItemView.setGrade((ImageView)convertView.findViewById(R.id.main_grade));
convertView.setTag(menuItemView); //设置控件集到convertView
}else {
menuItemView = (MainMenuItemView)convertView.getTag();
}
//设置文字和图片
menuItemView.getTitle().setText((String)listItems.get(position).get("title"));
menuItemView.getImage().setBackgroundResource((Integer)listItems.get(position).get("image"));
menuItemView.getScore().setText((String)listItems.get(position).get("score"));
menuItemView.getGrade().setBackgroundResource((Integer)listItems.get(position).get("grade"));
return convertView;
}
/**
* 初始化关卡信息
*/
private List<Map<String, Object>> getListItems() {
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for(int i = 0; i < title.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", title[i]); //关卡标题
map.put("image", imgeIDs[i]); //关卡图片
map.put("score", score[i]); //关卡分数
map.put("grade", gradeIDs[i]); //过卡等级
listItems.add(map);
}
return listItems;
}
}