近期做android应用遇到的一个棘手的问题:用网格视图将sdcard卡中的图片文件和图片名显示出来。我通过SQLite数据库将图片文件的名称和路径存储,然后查询数据库,取得路径再将图片读取出来保存到List<Map<String,Object>>中,然后封装到网格视图的adapter中。
出现的问题:图片名称可以显示,图片显示不出来。如何在该网格视图的每个网格中添加一个单选按钮。部分代码如下:List<Map<String, Object>> list = mpService.findPlateImgs();
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.customgridview,
new String[] { "image", "name" },
new int[] { R.id.masterplate_image, R.id.masterplate_text });
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {……}
说明:List<Map<String, Object>>该list存储内容:
Bitmap bitmap = getImage(imagePath);
map.put("image", bitmap);
map.put("name", name);
list.add(map);
R.layout.customgridview:该布局文件中包含一个ImageView和一个TextView。
求大虾指导!急!!!
出现的问题:图片名称可以显示,图片显示不出来。如何在该网格视图的每个网格中添加一个单选按钮。部分代码如下:List<Map<String, Object>> list = mpService.findPlateImgs();
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.customgridview,
new String[] { "image", "name" },
new int[] { R.id.masterplate_image, R.id.masterplate_text });
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {……}
说明:List<Map<String, Object>>该list存储内容:
Bitmap bitmap = getImage(imagePath);
map.put("image", bitmap);
map.put("name", name);
list.add(map);
R.layout.customgridview:该布局文件中包含一个ImageView和一个TextView。
求大虾指导!急!!!
map.put("image", bitmap);
改成map.put("imagePath",imagePath);
然后重写BaseAdapter
public View getView(int position, View convertView, ViewGroup parent) {
holder.icon.setImageDrawable(bitmap);
}
Google Android SDK开发范例大全里面 7.5有个例子,动态加载SD卡图片用BaseAdapter实现的。
final List<Map<String, Object>> list = mpService.findPlateImgs(mpService.findAllPlate());
// SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.customgridview,
// new String[] { "image", "name" },
// new int[] { R.id.masterplate_image, R.id.masterplate_text });
BaseAdapter adapter = new BaseAdapter(){ @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return position;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout layout = new RelativeLayout(MasterPlateActivity.this);
// layout.setLayoutParams(new ViewGroup.LayoutParams(
// ViewGroup.LayoutParams.WRAP_CONTENT, 180));
TextView tvPlateName = new TextView(MasterPlateActivity.this);
// tvPlateName.setLayoutParams(new ViewGroup.LayoutParams(
// ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageView imgvPlate = new ImageView(MasterPlateActivity.this);
RadioButton rbtnDefault = new RadioButton(MasterPlateActivity.this);
// imgvPlate.setLayoutParams(new ViewGroup.LayoutParams(
// ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// imgvPlate.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
Map<String, Object> map = list.get(position);
Bitmap bitmap = (Bitmap)map.get("image");
String name = (String)map.get("name");
imgvPlate.setImageBitmap(bitmap);
tvPlateName.setText(name);
// imgvPlate.setImageResource(images[position]);
layout.addView(tvPlateName);
layout.addView(imgvPlate);
layout.addView(rbtnDefault);
return layout;
}};
gridView.setAdapter(adapter);
大概的功能要求基本实现了,剩下的只剩下修改和布局
注释部分为测试用。
灰常感谢大家的帮助!