Android入门之ListView
2010-05-26 02:21:00 来源:WEB开发网{
HashMap< String, Object > map = new HashMap< String, Object >();
map.put("ItemImage", R.drawable.icon);//添加图像资源的ID
map.put("ItemTitle", "This is Title.....");
map.put("ItemText", "This is text.....");
lstImageItem.add(map);
}
//生成适配器的ImageItem < ==== > 动态数组的元素,两者一一对应
SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释
lstImageItem,//数据来源
R.layout.my_imageitem,//ListItem的XML实现
//动态数组与ImageItem对应的子项
new String[] {"ItemImage","ItemTitle", "ItemText"},
//ImageItem的XML文件里面的一个ImageView,两个TextView ID
new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});
//添加并且显示
list.setAdapter(saImageItems);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//绑定XML中的ListView,作为Item的容器
ListView list = (ListView) findViewById(R.id.MyListView);
//生成动态数组,并且转载数据
ArrayList< HashMap< String, Object > > lstImageItem = new ArrayList< HashMap< String, Object > >();
for(int i=0;i< 10;i++)
{
HashMap< String, Object > map = new HashMap< String, Object >();
map.put("ItemImage", R.drawable.icon);//添加图像资源的ID
map.put("ItemTitle", "This is Title.....");
map.put("ItemText", "This is text.....");
lstImageItem.add(map);
}
//生成适配器的ImageItem < ==== > 动态数组的元素,两者一一对应
SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释
lstImageItem,//数据来源
R.layout.my_imageitem,//ListItem的XML实现
//动态数组与ImageItem对应的子项
new String[] {"ItemImage","ItemTitle", "ItemText"},
//ImageItem的XML文件里面的一个ImageView,两个TextView ID
new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});
//添加并且显示
list.setAdapter(saImageItems);
}
更多精彩
赞助商链接