编写自己的Adapter

在用到LitView 或者GridView时,可以利用自己的写的适配器来设计界面:

 
public class GridAdapter extends BaseAdapter
{
	private List<Item> list;
	Activity testActivity;

	public GridAdapter(Context context, List<Item> list)
	{
		super();
		this.list = list;
		testActivity = (Activity) context;
	}

	@Override
	public int getCount()
	{
		return list.size();

	}

	@Override
	public Object getItem(int position)
	{
		return list.get(position);
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		Item item = list.get(position);
		if (convertView == null)
		{
			convertView = testActivity.getLayoutInflater().inflate(R.layout.grid_item, null);
		}
		ImageView itemImageView = (ImageView) convertView.findViewById(R.id.itemImage);
		itemImageView.setImageResource(item.getDraw());
		ImageView[] starImage = new ImageView[5];
		starImage[0] = (ImageView) convertView.findViewById(R.id.itemStars1);
		starImage[1] = (ImageView) convertView.findViewById(R.id.itemStars2);
		starImage[2] = (ImageView) convertView.findViewById(R.id.itemStars3);
		starImage[3] = (ImageView) convertView.findViewById(R.id.itemStars4);
		starImage[4] = (ImageView) convertView.findViewById(R.id.itemStars5);
		int num = item.getstars();
		for (int i = 0; i < 5; i++)
		{
			if (num > i)
			{
				starImage[i].setImageResource(R.drawable.grey);
			} else
			{
				starImage[i].setImageResource(R.drawable.yellow);
			}

		}
		return convertView;
	}
}
 
 
效果如下:
 
image 
posted @ 2011-09-01 09:26  图形学小菜鸟  阅读(313)  评论(0编辑  收藏  举报