android怎么用imageadapter代码实现图片的随机放置
1个回答
展开全部
在项目中用到SimpleAdapter 显示照片, 但是 SimpleAdapter 默认是静态显示程序内部的本地资源,即加载的是 R.drawable.id 这样的照片。如果我加载的是下载的Bitmap格式的图片,或者是SD卡自己存储的图片,那么这样是显示不出来的。
解决办法是:
实现 ViewBinder接口,对两种的资源id和bitmap 情况进行说明。
说明代码说下:
SimpleAdapter saImageItems;
// 生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
saImageItems = new SimpleAdapter(this,
lstImageItem, // 数据来源,也就是说网格视图里装载的数据内容
R.layout.gridview, // 网格视图里要显示的东西,这里是一副图像+下标文字
new String[] { "ItemImage", "ItemText" },// 动态数组与ImageItem对应的子项
new int[] { R.id.ItemImage, R.id.ItemText });// 显示ID号与上面的String[]元素一一对应
saImageItems.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(
View view,
Object data,
String textRepresentation) {
// TODO Auto-generated method stub
if((view instanceof ImageView) && (data instanceof Bitmap)) {
ImageView imageView = (ImageView) view;
Bitmap bmp = (Bitmap) data;
imageView.setImageBitmap(bmp);
return true;
}
return false;
}
});
expandListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
lstImageItem.clear();
ArrayList> kidsArrayList = new ArrayList>();
int classNum = (groupPosition+1)*100+childPosition+1;
kidsArrayList = MainTabActivity.returnDbClassInfo(classNum);
int studentNum = kidsArrayList.size();
for (int i = 0; i < studentNum; i++) {
HashMap map = new HashMap();
Bitmap bm = BitmapFactory.decodeFile(kidsArrayList.get(i).get("studentimage"));
map.put("ItemImage",bm);// 添加图像资源的ID
map.put("ItemText",kidsArrayList.get(i).get("name") );// 按序号做ItemTex
lstImageItem.add(map);
}
// 添加并且显示
gridview.setAdapter(saImageItems);
解决办法是:
实现 ViewBinder接口,对两种的资源id和bitmap 情况进行说明。
说明代码说下:
SimpleAdapter saImageItems;
// 生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
saImageItems = new SimpleAdapter(this,
lstImageItem, // 数据来源,也就是说网格视图里装载的数据内容
R.layout.gridview, // 网格视图里要显示的东西,这里是一副图像+下标文字
new String[] { "ItemImage", "ItemText" },// 动态数组与ImageItem对应的子项
new int[] { R.id.ItemImage, R.id.ItemText });// 显示ID号与上面的String[]元素一一对应
saImageItems.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(
View view,
Object data,
String textRepresentation) {
// TODO Auto-generated method stub
if((view instanceof ImageView) && (data instanceof Bitmap)) {
ImageView imageView = (ImageView) view;
Bitmap bmp = (Bitmap) data;
imageView.setImageBitmap(bmp);
return true;
}
return false;
}
});
expandListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
lstImageItem.clear();
ArrayList> kidsArrayList = new ArrayList>();
int classNum = (groupPosition+1)*100+childPosition+1;
kidsArrayList = MainTabActivity.returnDbClassInfo(classNum);
int studentNum = kidsArrayList.size();
for (int i = 0; i < studentNum; i++) {
HashMap map = new HashMap();
Bitmap bm = BitmapFactory.decodeFile(kidsArrayList.get(i).get("studentimage"));
map.put("ItemImage",bm);// 添加图像资源的ID
map.put("ItemText",kidsArrayList.get(i).get("name") );// 按序号做ItemTex
lstImageItem.add(map);
}
// 添加并且显示
gridview.setAdapter(saImageItems);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询