1. BaseAdapter的使用实例
BaseAdapter baseAdapter = new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(GridViewActivity.this); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); imageView.setPadding(5, 0, 5, 0); } else { imageView = (ImageView) convertView; } imageView.setImageResource(imageId[position]); return imageView; } @Override public long getItemId(int position) { return position; } @Override public Object getItem(int position) { return position; } @Override public int getCount() { return imageId.length; } };
2. SimpleAdapter的使用实例
public class GridViewActivity extends Activity { private int[] imageId = new int[] {R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09, R.drawable.img10, R.drawable.img11, R.drawable.img12, }; private String[] title = new String[] { "花开富贵", "海天一色", "日出", "天路", "一枝独秀", "云", "独占鳌头", "蒲公英花", "花团锦簇", "争奇斗艳", "和谐", "林间小路" }; // 定义并初始化保存说明文字的数组 private GridView gridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gridview); gridView = (GridView) findViewById(R.id.gridView1); List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>(); for (int i = 0; i < imageId.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("image", imageId[i]); map.put("title", title[i]); listItems.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.items, new String[] {"title", "image"}, new int[] {R.id.title, R.id.image}); gridView.setAdapter(adapter); } }
本文转载自android
《Android中常用适配器及定义自己的适配器》
参考网址:http://spring.wind2006.blog.163.com/blog/static/1205586520132231047411/