public class AdapterActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view); GridView gridView = (GridView) findViewById(R.id.gridView1); List> hashMapList = new ArrayList >(); //测试数据 for (int i = 0; i < 8; i++) { HashMap tempHashMap = new HashMap (); tempHashMap.put("item", getGridViewItem(1)); hashMapList.add(tempHashMap); } MyAdapter myAdapter = new MyAdapter(this, hashMapList); gridView.setAdapter(myAdapter); } public GridViewItem getGridViewItem(int page) { Bitmap tempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); GridViewItem tempItem = new GridViewItem(tempBitmap, "测试一下" + new Date().getSeconds()); return tempItem; }}
/** * GradView 项 * * @author Administrator * */public class GridViewItem { public Bitmap bitmap;// 图片 public String title;// 题标 // 待扩展 public GridViewItem() { } public GridViewItem(Bitmap bitmap, String title) { super(); this.bitmap = bitmap; this.title = title; }}
/** * * @author Administrator * */public class MyAdapter extends BaseAdapter { private List> list; private GridViewItem tempGridViewItem; private LayoutInflater layoutInflater; public MyAdapter(Context context, List > list) { this.list = list; layoutInflater = LayoutInflater.from(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) { View view = null; if (layoutInflater != null) { view = layoutInflater .inflate(R.layout.activity_gridview_item, null); ImageView imageView = (ImageView) view .findViewById(R.id.imageView1); TextView textView = (TextView) view.findViewById(R.id.title); //获取自定义的类实例 tempGridViewItem = list.get(position).get("item"); imageView.setImageBitmap(tempGridViewItem.bitmap); textView.setText(tempGridViewItem.title); } return view; }}
两个布局文件: