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("ItemText", "NO."+String.valueOf(i));//按序号做ItemText
lstImageItem.add(map);
}
2、 //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释
lstImageItem,//数据来源
R.layout.night_item,//night_item的XML实现
//动态数组与ImageItem对应的子项
new String[] {"ItemImage","ItemText"},
//ImageItem的XML文件里面的一个ImageView,两个TextView ID
new int[] {R.id.ItemImage,R.id.ItemText});
3、 //添加并且显示
gridview.setAdapter(saImageItems);
代码实现:
res/layout/third.xml 的源代码,就是它实现ImageItem的UI:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtv"
android:text="thrid activiy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<GridView
android:id="@+id/txtlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="90sp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
/>
</LinearLayout>
thirdlist.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="12dip"
android:paddingRight="12dip"
android:paddingBottom="4dip"
>
<ImageView
android:paddingTop="12dip"
android:contentDescription="alter img"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemimage"
/>
<TextView
android:id="@+id/itemtitle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/itemtext"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
android:layout_below="@id/itemtitle"
/>
</RelativeLayout>
//java
package com.example.helloword;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ThirdActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
GridView lv = (GridView)findViewById(R.id.txtlist);
//生成动态数组,加入数据
ArrayList<HashMap<String,Object>> listItem = new ArrayList<HashMap<String,Object>>();
for(int i = 0;i < 10 ;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("image", R.drawable.p1);
map.put("title", "itemtitle" + i);
map.put("text", "itemtext" + i);
listItem .add(map);
}
////生成适配器的Item和动态数组对应的元素
SimpleAdapter sadapter = new SimpleAdapter(this,
listItem ,//数据源
R.layout.thirdlist,//xml
new String[]{"image","title","text"},//动态数组与ImageItem对应的子项
new int[]{ R.id.itemimage,R.id.itemtitle,R.id.itemtext });
//添加并且显示
lv.setAdapter(sadapter);
}
}