1个简单的SimpleAdapter工具类-SimpleAdapterUtil
根据android.widget.SimpleAdapter来生成对应需要装备的适配器。
代码:
版本一:数据格式为List<Object[]>
package com.zhouzijing.android.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.widget.SimpleAdapter; public class SimpleAdapterUtil{ /** * 创建适配器并配置数据. * @param context The context where the View associated with this SimpleAdapter is running * @param item 需要绑定的数据 * @param resource resource Resource identifier of a view layout that defines the views for this list * item. The layout file should include at least those named views defined in "to" * @param to The views that should display column in the "from" parameter. These should all be * TextViews. The first N views in this list are given the values of the first N columns * in the from parameter. * @return */ public static SimpleAdapter create(Context context,List<Object[]> item, int resource,int[] to) { //定义适配器内容数据 List<Map<String,?>> data = new ArrayList<Map<String,?>>(); //获取要显示内容数量 int itemLength = item.get(0).length; //获取要显示内容内部长度 int itemCount = item.size(); //定义适配器from参数 String[] adapterFroms = new String[itemCount]; for(int i=0;i<itemCount;i++){ adapterFroms[i]="Item"+String.valueOf(i); } //准备适配器内容 for(int i=0;i<itemLength;i++){ Map<String,Object> map = new HashMap<String,Object>(); for(int j=0;j<itemCount;j++){ map.put(adapterFroms[j], item.get(j)[i]); } data.add(map); } //绑定适配器并返回 SimpleAdapter simpleAdapter = new SimpleAdapter(context, data, resource, adapterFroms, to); return simpleAdapter; } }
版本二:数据格式为ArrayList<ArrayList<Object>>
package com.zhouzijing.android.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.widget.SimpleAdapter; public class SimpleAdapterUtil{ /** * 创建适配器并配置数据. * @param context The context where the View associated with this SimpleAdapter is running * @param item 需要绑定的数据 * @param resource resource Resource identifier of a view layout that defines the views for this list * item. The layout file should include at least those named views defined in "to" * @param to The views that should display column in the "from" parameter. These should all be * TextViews. The first N views in this list are given the values of the first N columns * in the from parameter. * @return */ public static SimpleAdapter create(Context context,ArrayList<ArrayList<Object>> item, int resource,int[] to) { //定义适配器内容数据 List<Map<String,?>> data = new ArrayList<Map<String,?>>(); //获取要显示内容数量 int itemLength = item.get(0).size(); //获取要显示内容内部长度 int itemCount = item.size(); //定义适配器from参数 String[] adapterFroms = new String[itemCount]; for(int i=0;i<itemCount;i++){ adapterFroms[i]="Item"+String.valueOf(i); } //准备适配器内容 for(int i=0;i<itemLength;i++){ Map<String,Object> map = new HashMap<String,Object>(); for(int j=0;j<itemCount;j++){ map.put(adapterFroms[j], item.get(j).get(i)); } data.add(map); } //绑定适配器并返回 SimpleAdapter simpleAdapter = new SimpleAdapter(context, data, resource, adapterFroms, to); return simpleAdapter; } }