Cua*_*uiu 6 android android-listview android-imageview
我有一个自定义游标适配器,我想将一个图像放入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);
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都好,但我想从数据库(光标)获取图像的名称.我试过了
String mDrawableName = "myImageName";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Run Code Online (Sandbox Code Playgroud)
但返回错误:"方法getResources()未定义类型CustomImageListAdapter"
MH.*_*MH. 13
您只能getResources()对Context对象进行调用.由于CursorAdapter构造函数采用了这样的引用,只需创建一个跟踪它的类成员,以便您可以使用它(大概)bindView(...).你可能也需要它getPackageName().
private Context mContext;
public CustomImageListAdapter(Context context, Cursor cursor) {
super(context, cursor);
inflater = LayoutInflater.from(context);
mContext = context;
}
// Other code ...
// Now call getResources() on the Context reference (and getPackageName())
String mDrawableName = "myImageName";
int resID = mContext.getResources().getIdentifier(mDrawableName , "drawable", mContext.getPackageName());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10073 次 |
| 最近记录: |