我有一个自定义的光标适配器,我想将图像放入ListView中的ImageView。通过名称获取资源图像到自定义光标适配器
我的代码是:
public class CustomImageListAdapter extends CursorAdapter {
private LayoutInflater inflater;
public CustomImageListAdapter(Context context, Cursor cursor) {
super(context, cursor);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// get the ImageView Resource
ImageView fieldImage = (ImageView) view.findViewById(R.id.fieldImage);
// set the image for the ImageView
flagImage.setImageResource(R.drawable.imageName);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.row_images, parent, false);
}
}
这是一切OK,但我想从数据库(光标)获取图像的名称。 我试着用
String mDrawableName = "myImageName";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
但返回错误:“该方法getResources()是未定义的类型CustomImageListAdapter”
如果你想从光标获得,为什么不改用'cursor.getString'。你的图像存储在哪里? – 2012-03-04 00:20:25