欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义Adapter写法记录

程序员文章站 2022-07-15 09:16:04
...

 

package com.example.coolvidaoplayer;

import java.util.List;
import java.util.Map;

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;

public class VidaoListAdapter extends BaseAdapter {
	private LayoutInflater videoListInflater;
	private List<Map<String,Object>>videoList;
	public VidaoListAdapter(Context context,List<Map<String,Object>>list)
	{
		this.videoListInflater=LayoutInflater.from(context);
		this.videoList=list;
	}
	static class ViewHolder{
		ImageView ivVideoThumbnail;
		TextView tvVideoName;
		TextView tvVideoSize;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(videoList!=null)
		{
			return videoList.size();
		}else
		return 0;
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return videoList.get(pos);
	}

	@Override
	public long getItemId(int id) {
		// TODO Auto-generated method stub
		return id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder videoHolder=null;
		if(convertView==null)
		{
			videoHolder=new ViewHolder();
			convertView=videoListInflater.inflate(R.layout.list_item_vidaoinfo, null);
			videoHolder.ivVideoThumbnail=(ImageView)convertView.findViewById(R.id.iv_vidao_thumbnail);
			videoHolder.tvVideoName=(TextView)convertView.findViewById(R.id.tv_vidao_name);
			videoHolder.tvVideoSize=(TextView)convertView.findViewById(R.id.tv_vidao_size);
			videoHolder.ivVideoThumbnail.setBackgroundResource((Integer)videoList.get(position).get("videothumbnail"));
			videoHolder.tvVideoName.setText((String)videoList.get(position).get("videoname"));
			videoHolder.tvVideoSize.setText((String)videoList.get(position).get("videosize"));
			convertView.setTag(videoHolder);
		}
		else
		{
			videoHolder=(ViewHolder)convertView.getTag();
		}
		
		return convertView;

	}

}