除了使用BaseAdapter之外,SimpleAdapter也是一种最为常见的适配器操作类。
既然要使用SimpleAdapter类,那么就需要准备一个List集合,而在这个List集合之中,
一定要保存Map集合,而且Map集合的key必须是String。
既然要设置适配器,那么就要把需要的所有图片资源都进行显示。现在只有少量的图片,如果现在的图片很多呢,例如100张,按照定义100个长度的数组并不可取。
在java中存在一种发射机制,此时可以利用发射机制进行动态加载。
如果需要使用SimpleAdater肯定需要定义一个内部的组件,这个是一个布局文件。
在main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/myGallery"
android:gravity="center_vertical"
android:spacing="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
在res/layout中新建grid_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="70dp"
android:scaleType="center"/>
</LinearLayout>
在MyGalleryProjectDemo.java程序中
package com.tarena.galleryproject;
import java.text.DateFormat.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.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Gallery;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MyGalleryProjectDemo extends Activity {
private Gallery gallery = null;
private SimpleAdapter simpleAdapter = null;
private List<Map<String, Integer>> list = new
ArrayList<Map<String,Integer>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.initAdapter();
this.initAdapter();
this.gallery = (Gallery) super.findViewById(R.id.myGallery);
this.gallery.setAdapter(this.simpleAdapter);
this.gallery.setOnItemClickListener(new onItemClickListenerImpl());
}
private void initAdapter(){
java.lang.reflect.Field[] fields = R.drawable.class.getDeclaredFields(); //取得全部的属性
for(int x = 0; x < fields.length; x++){
if(fields[x].getName().startsWith("ispic_")){ //我们所需要的图片,图片的头文件名为ispic_
Map<String,Integer> map = new HashMap<String, Integer>();
try {
map.put("img", fields[x].getInt(R.drawable.class)); //必须定义好名称是img
} catch (Exception e) {
e.printStackTrace();
}
this.list.add(map);
}
}
this.simpleAdapter = new SimpleAdapter(this,this.list,
R.layout.grid_layout,new String[]{"img"},
new int[]{R.id.img});
}
private class onItemClickListenerImpl implements OnItemClickListener{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MyGalleryProjectDemo.this,
String.valueOf(position),
Toast.LENGTH_SHORT).show();
}
}
}