android 重写adpter

重写系统的simpleadpter:
public class ListSimpleAdpter extends SimpleAdapter{
   //要使用到的数据源
    private List<Map<String, Object>> data=new ArrayList<Map<String, Object>>();
   //填充数据的资源文件
    private int resource;
    private String[] from;
    private Context context;
    public ListSimpleAdpter(Context context,
            List<Map<String, Object>> data, int resource, String[] from,
            int[] to) {
        super(context, data, resource, from, to);
        this.context=context;
        this.data=data;
        this.resource=resource;
        this.from=from;
    }
    //item的总行数
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data==null?0:data.size();
    }
    //item对象
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    //item的id
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
   //绘制每一个item
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder = null;
        if(convertView==null)
        {
           convertView=LayoutInflater.from(context).inflate(resource, null);
          holder=new Holder();
          holder.imageView=(ImageView) convertView.findViewById(R.id.listitem_pic);
            holder.title=(TextView) convertView.findViewById(R.id.listitem_title);
            holder.content=(TextView) convertView.findViewById(R.id.listitem_content);
            convertView.setTag(holder);
        }else
        {
            holder=(Holder) convertView.getTag();
        }
        holder.imageView.setImageResource(Integer.parseInt(data.get(position).get(from[0]).toString()));
        holder.title.setText(data.get(position).get(from[1]).toString());
        holder.content.setText(data.get(position).get(from[2]).toString());
        return convertView;
    }
   
    class Holder{
        ImageView imageView;
        TextView title;
        TextView content;
    }
}   这里对于Adapter的优化,用的很古老的ViewHolder、ViewCache办法:public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder = null;
        if(convertView==null)
        {
            convertView=LayoutInflater.from(context).inflate(resource, null);
            holder=new Holder();
            holder.imageView=(ImageView) convertView.findViewById(R.id.listitem_pic);
            holder.title=(TextView) convertView.findViewById(R.id.listitem_title);
            holder.content=(TextView) convertView.findViewById(R.id.listitem_content);
            convertView.setTag(holder);
        }else
        {
            holder=(Holder) convertView.getTag();
        }
        holder.imageView.setImageResource(Integer.parseInt(data.get(position).get(from[0]).toString()));
        holder.title.setText(data.get(position).get(from[1]).toString());
        holder.content.setText(data.get(position).get(from[2]).toString());
        return convertView;
    }
   
    class Holder{
        ImageView imageView;
        TextView title;
        TextView content;
    }或者使用HashMap做缓存的方法:HashMap<Integer, View> m = new HashMap<Integer, View>();

public View getView(int position, View view, ViewGroup parent) {

  View convertView = m.get(position);
  if (convertView != null) {
    return convertView;
  } else {  
  convertView=LayoutInflater.from(context).inflate(resource, null);
  ImageView imageView=(ImageView) convertView.findViewById(R.id.listitem_pic);
  TextView title=(TextView) convertView.findViewById(R.id.listitem_title);
  TextView content=(TextView) convertView.findViewById(R.id.listitem_content);
   m.put(position, convertView);
   }
}

你可能感兴趣的:(android)