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;
}
}
月落乌啼落日圆
相关分类