GridView动态增加图片

  1. package com.cloudxp;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;

  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.AdapterView.OnItemClickListener;
  11. import android.widget.GridView;
  12. import android.widget.ListAdapter;
  13. import android.widget.SimpleAdapter;

  14. public class EditCourseIcon extends Activity {

  15. private GridView mygridview;
  16. private List<Map<String, Object>> items = new ArrayList<Map<String,Object>>();
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. // TODO Auto-generated method stub
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.editcourseicon);

  22. //准备要添加的数据条目

  23. for (int i = 0; i < 10; i++) {
  24. Map<String, Object> item = new HashMap<String, Object>();
  25. item.put("imageItem", R.drawable.icon);
  26. item.put("textItem", "text" + i);
  27. items.add(item);
  28. }
  29. //实例化一个适配器
  30. SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.editcourseiconitem, new String[]{"imageItem", "textItem"}, new int[]{R.id.image_item, R.id.text_item});
  31. //获得GridView实例
  32. mygridview = (GridView) findViewById(R.id.mygridview);
  33. //gridview.setNumColumns(3);//可以在xml中设置
  34. //gridview.setGravity(Gravity.CENTER);//同上
  35. //将GridView和数据适配器关联
  36. mygridview.setAdapter(adapter);

  37. //设置监听器
  38. mygridview.setOnItemClickListener(new ItemClickListener());
  39. }
  40. class ItemClickListener implements OnItemClickListener
  41. {

  42. @Override
  43. public void onItemClick(AdapterView<?> parent, View view, int position,
  44. long id) {
  45. // TODO Auto-generated method stub
  46. System.out.println(position);
  47. System.out.println(id);
  48. parent.getItemAtPosition(position);
  49. System.out.println(parent.getItemAtPosition(position));

  50. /*public void UpdateList(int selectedItem)
  51. {
  52. ListAdapter la = m_List.getAdapter();
  53. int itemNum = m_List.getCount();
  54. for(int i=0; i<itemNum; i++)
  55. {
  56. HashMap<String, Object> map = (HashMap<String, Object>)la.getItem(i);
  57. if ( i == selectedItem)
  58. map.put("ItemIcon", R.drawable.image1);
  59. else
  60. map.put("ItemIcon", R.drawable.image2);
  61. }

  62. ((SimpleAdapter)la).notifyDataSetChanged();
  63. }*/
  64. if(position==parent.getCount()-1)
  65. {
  66. ListAdapter la = mygridview.getAdapter();
  67. HashMap<String, Object> item = new HashMap<String, Object>();
  68. item.put("imageItem", R.drawable.yw);
  69. int i=position+1;
  70. item.put("textItem", "text" + i);
  71. items.add(item);
  72. ((SimpleAdapter)la).notifyDataSetChanged();
  73. }
  74. Object lastObject = parent.getItemAtPosition(position+1);
  75. Object object = parent.getItemAtPosition(position);
  76. items.set(position, (Map<String, Object>)lastObject);
  77. items.set(position+1, (Map<String, Object>)object);
  78. }

  79. }
  80. }
from:http://www.apkbus.com/android-6013-1-1.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值