开发学院手机开发Android 开发 Android GridView 的使用 阅读

Android GridView 的使用

 2010-04-21 06:51:00 来源:WEB开发网   
核心提示:2. super.onCreate(savedInstanceState);3. setContentView(R.layout.main);4. GridView gridview = (GridView) findViewById(R.id.gridview);5.6. //生成动态数组,并且转入数据7. Arra

2. super.onCreate(savedInstanceState);

3. setContentView(R.layout.main);

4. GridView gridview = (GridView) findViewById(R.id.gridview);

5.

6. //生成动态数组,并且转入数据

7. ArrayList< HashMap< String, Object > > lstImageItem = new ArrayList< HashMap< String, Object > >();

8. for(int i=0;i< 10;i++)

9. {

10. HashMap< String, Object > map = new HashMap< String, Object >();

11. map.put("ItemImage", R.drawable.icon);//添加图像资源的ID

12. map.put("ItemText", "NO."+String.valueOf(i));//按序号做ItemText

13. lstImageItem.add(map);

14. }

15. //生成适配器的ImageItem < ==== > 动态数组的元素,两者一一对应

16. SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释

17. lstImageItem,//数据来源

18. R.layout.night_item,//night_item的XML实现

19.

20. //动态数组与ImageItem对应的子项

21. new String[] {"ItemImage","ItemText"},

22.

23. //ImageItem的XML文件里面的一个ImageView,两个TextView ID

24. new int[] {R.id.ItemImage,R.id.ItemText});

25. //添加并且显示

26. gridview.setAdapter(saImageItems);

27. //添加消息处理

28. gridview.setOnItemClickListener(new ItemClickListener());

29. }

30.

31. //当AdapterView被单击(触摸屏或者键盘),则返 回的Item单击事件

32. class ItemClickListener implements OnItemClickListener

33. {

34. public void onItemClick(AdapterView< ? > arg0,//The AdapterView where the click happened

35. View arg1,//The view within the AdapterView that was clicked

36. int arg2,//The position of the view in the adapter

37. long arg3//The row id of the item that was clicked

38. ) {

39. //在本例中arg2=arg3

40. HashMap< String, Object > item=(HashMap< String, Object >) arg0.getItemAtPosition(arg2);

41. //显示所选Item的ItemText

42. setTitle((String)item.get("ItemText"));

43. }

44.

45. }

上一页  1 2 3 

Tags:Android GridView 使用

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接