两个xml:main.xml
l:
demo.xml
java源码:
/**
* 数据*/
public List<Map<String, Object>> data(){
List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
for(int i=0;i<10;i++){
Map<String, Object> map=new HashMap<String, Object>();
map.put("item", R.drawable.ic_launcher);
map.put("itemText", "NO."+i);
list.add(map);
}
return list;
}
/**
* gridView组件配置*/
public void gridViewTest(){
//生成适配器
SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,data(), R.layout.demo, new String[]{"item","itemText"}, new int[]{R.id.imageView,R.id.textView});
GridView gridView=(GridView) findViewById(R.id.gridView);
//添加并显示
gridView.setAdapter(adapter);
//添加消息处理机制
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,//The AdapterView where the click happened
View view,//The view within the AdapterView that was clicked
int position, //The position of the view in the adapter
long id//The row id of the item that was clicked
) {
// TODO Auto-generated method stub
Map map=(Map) parent.getItemAtPosition(position);
setTitle((String)map.get("itemText"));}
});
}