xml
1 19 9 android:numColumns="auto_fit" 10 10 android:columnWidth="70dp" 11 11 android:stretchMode="columnWidth" 12 12 android:verticalSpacing="10dp" >13 13
activity中的代码
public class heroActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.hero); GridView gridview = (GridView) findViewById(R.id.gridview); ArrayList> lstimage = new ArrayList >(); for (int i = 0; i < arr.nums(); i++) { HashMap map = new HashMap (); map.put("image", arr.getimage(i)); map.put("text", arr.getname(i)); lstimage.add(map); } //将英雄头像和名称添加到动态数组 lstimage 中 SimpleAdapter saImageItems = new SimpleAdapter(this, lstimage, R.layout.gv, new String[] { "image", "text" }, new int[] { R.id.image, R.id.text }); gridview.setAdapter(saImageItems); //设置gridview的适配器 gridview.setOnItemClickListener(new ItemClickListener()); Button bask = (Button) findViewById(R.id.bask); bask.getBackground().setAlpha(100); bask.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Toast.makeText(heroActivity.this, "付费后开通查询功能 >.<", Toast.LENGTH_LONG) .show(); } }); } class ItemClickListener implements OnItemClickListener { public void onItemClick(AdapterView parent, View view, int position, long rowid) { HashMap item = (HashMap ) parent .getItemAtPosition(position); // 获取数据源的属性值 String itemText = (String) item.get("text"); Object object = item.get("image"); //获取所点击的英雄头像(image)和名称(text) Toast.makeText(heroActivity.this, itemText, Toast.LENGTH_LONG) .show(); Intent intent = new Intent(heroActivity.this, herodata.class); intent.putExtra("position", position); startActivity(intent); } }}