代码:
private int[] imageId=new int[]{ R.drawable.img01,R.drawable.img02,R.drawable.img03,R.drawable.img04,
R.drawable.img05,R.drawable.img06,R.drawable.img07,R.drawable.img08,
R.drawable.img09,R.drawable.img10,R.drawable.img11,R.drawable.img12
};
private String[]title = new String[]{"1","2","3","4",
"1","2","3","4","1","2","3","4"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView girdview=(GridView)findViewById(R.id.gridView1);
List<Map<String,Object>>listItems=new ArrayList<Map<String,Object>>();
// 通过for循环将图片ID和列表文字放到MAP中,并添加到List集合中
for(int i=0;i<imageId.length;i++){
Map<String,Object>map=new HashMap<String,Object>();
map.put("image",imageId[i]);
map.put("title", title[i]);
listItems.add(map);
}
SimpleAdapter adapter=new SimpleAdapter(this, listItems, R.layout.items,
new String[]{"title","image"},new int[]{android.R.id.title,R.id.image});
girdview.setAdapter(adapter);
}
显示:
效果如下: