MyAdapter的初体验

package com.example.androidtest;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
//继承BaseAdapter
public class MyAdapter extends BaseAdapter{
	 
	private LayoutInflater mLayoutInflater;
	//定义数据和承接上下文的参数
	private Context mConext;
	private List mData;
	//构造函数引入两个参数
	public  MyAdapter(Context mConext , List mData){
		mLayoutInflater = LayoutInflater.from(mConext);
		this.mConext = mConext;
		this.mData = mData;
		
	}

	@Override
	public int getCount() {
		//数据不为空则返回数据的大小
		return (mData == null) ? 0 : mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		//返回数据
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		//返回数据的地址
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//可以直接调用tv_title
		ViewHolder holder;
		//如果convertView为空,则建立视图
		if (convertView == null) {
			holder = new ViewHolder();
			//使用inFlater方法来加载一个布局,用于ListViw的每一个Item的布局
			convertView = mLayoutInflater.inflate(R.layout.cell, parent, false);
			//用convertView来找到tv_title的Id
			holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			//holder.tv = (TextView) convertView.findViewById(R.id.tv);
			
			convertView.setTag(holder);
			//convertView.setTag(holder);
			
			
		}
		else{
			holder = (ViewHolder) convertView.getTag();
			//holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_title.setText(mData.get(position));
		return convertView;
	}
	
	class ViewHolder{
		private TextView tv_title;
	}

}

你可能感兴趣的:(android学习)