Android Adapter的几种用法

版权声明:转载请注明出处:https://blog.csdn.net/zwjemperor https://blog.csdn.net/zwjemperor/article/details/7752052
1、继承BaseAdapter
public class ListViewAdapter extends BaseAdapter
{
	LayoutInflater inflater;
	private Context context;
	private String[] content;
	private int count;

	public ListViewAdapter(Context context, String[] content)
	{
		this.content = content;
		this.context = context;
		this.count = content.length;
		inflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount()
	{
		return count;
	}

	public void setCount(int count)
	{
		if (count > content.length)
			this.count = content.length;
		else if (count > 0)
			this.count = count;
	}

	@Override
	public Object getItem(int position)
	{
		return content[position];
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		TextView txtView = null;
		if (convertView == null)
			convertView = inflater.inflate(R.layout.listitem, null);
		txtView = (TextView) convertView.findViewById(R.id.listcontent);
		txtView.setText(content[position]);
		return convertView;
	}
}


2、分部绑定,其实就是控制adapter的getCount()函数返回的元素个数
		//创建listView的适配器
		final ListViewAdapter listAdapter = new ListViewAdapter(this, content);
		if (content.length > countForInit)//分组数大于countForInit
		{
			LayoutInflater inflater = LayoutInflater.from(this);
			View view = inflater.inflate(R.layout.listitem, null);
			view.setBackgroundResource(R.drawable.down);
			//add footer View
			listView.addFooterView(view);
			//设置初始显示个数
			listAdapter.setCount(countForInit);
			listView.setAdapter(listAdapter);
			listView.setOnItemClickListener(new OnItemClickListener()
			{
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id)
				{
					if (position == listAdapter.getCount())//判断点击的是否是footer
					{
						//每次增加countForInit个需要显示的元素
						listAdapter.setCount(listAdapter.getCount() + countForInit);
						//通知Adpter数据已改变
						listAdapter.notifyDataSetChanged(); 
						//所有分组已显示完,移除footer
						if (listAdapter.getCount() == content.length)
							listView.removeFooterView(view);
					}
				}
			});
public class ListViewAdapter extends BaseAdapter
{
	private Context context;
	private String[] content;
	private String[] contentid;
	private int count;

	public ListViewAdapter(Context context, String[] content, String[] contentid)
	{
		this.content = content;
		this.context = context;
		this.contentid = contentid;
		this.count = content.length;
	}

	@Override
	public int getCount()
	{
		return count;
	}

	public void setCount(int count)
	{
		if (count > content.length)
			this.count = content.length;
		else if (count > 0)
			this.count = count;
	}

	@Override
	public Object getItem(int position)
	{
		return content[position];
	}

}
3、继承SimpleCursorAdapter(绑定图片)
SimpleCursorAdapter adapter = new PersonAdapter(this, R.layout.person_item, cursor, new String[] { "_id", "pname", "pgender" }, new int[] { R.id.tvPid, R.id.tvPname, R.id.ivPgender });
lvPerson.setAdapter(adapter);
class PersonAdapter extends SimpleCursorAdapter
{
	private Cursor mCursor;
	protected int[] mFrom;
	protected int[] mTo;

	private ViewBinder mViewBinder;

	public PersonAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
	{
		super(context, layout, c, from, to);
		mCursor = c;
		mTo = to;
		findColumns(from);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor)
	{
		final ViewBinder binder = mViewBinder;
		final int count = mTo.length;
		final int[] from = mFrom;
		final int[] to = mTo;

		for (int i = 0; i < count; i++)
		{
			final View v = view.findViewById(to[i]);
			if (v != null)
			{
				boolean bound = false;
				if (binder != null)
					bound = binder.setViewValue(v, cursor, from[i]);

				if (!bound)
				{
					String text = cursor.getString(from[i]);
					if (text == null)
						text = "";
					if (v instanceof TextView)
						setViewText((TextView) v, text);
					else if (v instanceof ImageView)
					{
						if (text.equals("男"))
							setViewImage((ImageView) v, String.valueOf(R.drawable.boy));
						else
							setViewImage((ImageView) v, String.valueOf(R.drawable.girl));
					}
					else
						throw new IllegalStateException(v.getClass().getName() + " is not a " + " view that can be bounds by this SimpleCursorAdapter");
				}
			}
		}
	}

	private void findColumns(String[] from)
	{
		if (mCursor != null)
		{
			int i;
			int count = from.length;
			if (mFrom == null || mFrom.length != count)
				mFrom = new int[count];
			for (i = 0; i < count; i++)
				mFrom[i] = mCursor.getColumnIndexOrThrow(from[i]);
		}
		else
			mFrom = null;
	}
}


4、继承BaseAdapter,实现绑定图片等控件
public class ViewHolderAdapter extends BaseAdapter
{
	private LayoutInflater mInflater;
	private List<Map<String, Object>> mData;
	public static Map<Integer, Boolean> isSelected;

	public ViewHolderAdapter (Context context)
	{
		mInflater = LayoutInflater.from(context);
		init();
	} 

	// 初始化
	private void init()
	{
		mData = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < 5; i++)
		{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("img", R.drawable.icon);
			map.put("title", "第" + (i + 1) + "行的标题");
			mData.add(map);
		}
		// 这儿定义isSelected这个map是记录每个listitem的状态,初始状态全部为false。
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < mData.size(); i++)
		{
			isSelected.put(i, false);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder = null; // convertView为null的时候初始化convertView。
		if (convertView == null)
		{
			holder = new ViewHolder();
			convertView = mInflater.inflate(1, null);
			holder.img = (ImageView) convertView.findViewById(2);
			holder.title = (TextView) convertView.findViewById(3);
			holder.cBox = (CheckBox) convertView.findViewById(4);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.img.setBackgroundResource((Integer) mData.get(position).get("img"));
		holder.title.setText(mData.get(position).get("title").toString());
		holder.cBox.setChecked(isSelected.get(position));
		return convertView;
	}

	public final class ViewHolder
	{
		public ImageView img;
		public TextView title;
		public CheckBox cBox;
	}
}


 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自blog.csdn.net/zwjemperor/article/details/7752052