public class MainActivity extends Activity { private GridView gv; private SimpleAdapter sim_adapter; private List<Map<String,Object>>dataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gv=(GridView) findViewById(R.id.gridView1); /* * 1、准备数据源 * 2、新建适配器 * 3、GridView加载适配器 * 4、GridView配置事件监听器 * 5、视图加载适配器 */ dataList=new ArrayList<Map<String,Object>>(); sim_adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[]{"img","text"}, new int[]{R.id.imageView1,R.id.textView1}); gv.setAdapter(sim_adapter); } private List<Map<String,Object>> getData(){ int[] img={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6}; String[] imgName={"图片1","图片2","图片3","图片4","图片5","图片6"}; Map<String,Object>map=new HashMap<String, Object>(); for(int i=0;i<img.length;i++){ map.put("img", img[i]); map.put("text", imgName[i]); dataList.add(map); } return dataList; }}
1 回答
已采纳
月落乌啼落日圆
TA贡献20条经验 获得超9个赞
你把map的声明放到for循环里面就对了,像下面这样,因为如果不每次都声明一个map的话,你所添加的就会是同一个对象
for(int i=0;i<img.length;i++){ Map<String,Object>map=new HashMap<String, Object>(); map.put("img", img[i]); map.put("text", imgName[i]); dataList.add(map); }
- 1 回答
- 1 关注
- 1320 浏览
添加回答
举报
0/150
提交
取消