除了使用BaseAdapter之外,SimpleAdapter也是一种最为常见的适配器操作类。
既然决定要使用SimpleAdapter类了,那么就需要为其准备一个List集合,而在这个List集合之中,一定要保存Map集合,而且Map集合的key必须是String。

 

grid_layout.xml

控件:Gallery --- 2.(SimpleAdapter)_java控件:Gallery --- 2.(SimpleAdapter)_map集合_02View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:background
="#FFFFFF">
<ImageView
android:id="@+id/img"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:scaleType
="center"/>
</LinearLayout>

 

main.xml

控件:Gallery --- 2.(SimpleAdapter)_java控件:Gallery --- 2.(SimpleAdapter)_map集合_02View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id
="@+id/MyLayout"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent">
<Gallery
android:id="@+id/myGallery"
android:gravity
="center_vertical"
android:spacing
="3px"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"/>
</LinearLayout>

 

MyGalleryDemo.java

控件:Gallery --- 2.(SimpleAdapter)_java控件:Gallery --- 2.(SimpleAdapter)_map集合_02View Code
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MyGalleryDemo extends Activity {
// 图片浏览
private Gallery gallery = null;
private List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>() ;
// 适配器
private SimpleAdapter simpleAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 调用布局文件
super.setContentView(R.layout.main);
// 初始化适配器
this.initAdapter() ;
// 取得组件
this.gallery = (Gallery) super.findViewById(R.id.myGallery) ;
// 设置图片集
this.gallery.setAdapter(this.simpleAdapter);
// 设置单击事件
this.gallery.setOnItemClickListener(new OnItemClickListenerImpl()) ;
}
private class OnItemClickListenerImpl implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// 显示图片编号
Toast.makeText(MyGalleryDemo.this,
"" + position, Toast.LENGTH_SHORT).show();
}
}

// 初始化适配器
public void initAdapter(){
Field[] fields = R.drawable.class.getDeclaredFields();
for (int x = 0; x < fields.length; x++) {
// 所有ispic_*命名的图片
if (fields[x].getName().startsWith("ispic_")){
// 定义Map
Map<String,Integer> map = new HashMap<String,Integer>() ;
try {
// 设置图片资源
map.put("img", fields[x].getInt(R.drawable.class)) ;
} catch (Exception e) {
}
this.list.add(map) ; // 保存Map
}
}
// 实例化SimpleAdapter
this.simpleAdapter = new SimpleAdapter(
this,
this.list, // 要包装的数据集合
R.layout.grid_layout, // 要使用的显示模板
new String[] { "img" }, // 定义要显示的Map的Key
new int[] {R.id.img }); // 与模板中的组件匹配
}
}