怎么在Android应用中利用ViewHolder优化Adapter-创新互联

怎么在Android应用中利用ViewHolder优化Adapter?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联成立与2013年,先为砚山等服务建站,砚山等地企业,进行企业商务咨询服务。为砚山企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

具体方法如下:

public class MarkerItemAdapter extends BaseAdapter
{
  private Context mContext = null;
  private List mMarkerData = null;

  public MarkerItemAdapter(Context context, List markerItems)
  {
    mContext = context;
    mMarkerData = markerItems;
  }

  public void setMarkerData(List markerItems)
  {
    mMarkerData = markerItems;
  }

  @Override
  public int getCount()
  {
    int count = 0;
    if (null != mMarkerData)
    {
      count = mMarkerData.size();
    }
    return count;
  }

  @Override
  public MarkerItem getItem(int position)
  {
    MarkerItem item = null;

    if (null != mMarkerData)
    {
      item = mMarkerData.get(position);
    }

    return item;
  }

  @Override
  public long getItemId(int position)
  {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    ViewHolder viewHolder = null;
    if (null == convertView)
    {
      viewHolder = new ViewHolder();
      LayoutInflater mInflater = LayoutInflater.from(mContext);
      convertView = mInflater.inflate(R.layout.item_marker_item, null);

      viewHolder.name = (TextView) convertView.findViewById(R.id.name);
      viewHolder.description = (TextView) convertView
          .findViewById(R.id.description);
      viewHolder.createTime = (TextView) convertView
          .findViewById(R.id.createTime);

      convertView.setTag(viewHolder);
    }
    else
    {
      viewHolder = (ViewHolder) convertView.getTag();
    }

    // set item values to the viewHolder:

    MarkerItem markerItem = getItem(position);
    if (null != markerItem)
    {
      viewHolder.name.setText(markerItem.getName());
      viewHolder.description.setText(markerItem.getDescription());
      viewHolder.createTime.setText(markerItem.getCreateDate());
    }

    return convertView;
  }

  private static class ViewHolder
  {
    TextView name;
    TextView description;
    TextView createTime;
  }

}

文章名称:怎么在Android应用中利用ViewHolder优化Adapter-创新互联
链接分享:http://scjbc.cn/article/cdiscp.html

其他资讯