Android 使用SimpleCursorAdapter在列表视图中设置图像

Android 使用SimpleCursorAdapter在列表视图中设置图像,android,listview,Android,Listview,我想在ListView中设置图像,我的目的是从数据库中获取一个包含图像地址的字符串(例如“@drawable/image”),并为LisView的元素设置图像。我已经创建了一个自定义SimpleCursorAdapter来更改字体。这是我的自定义SimpleCursorAdapter代码: public class CustomFontExampleAdapter extends SimpleCursorAdapter { private Typeface mCustomFont;

我想在ListView中设置图像,我的目的是从数据库中获取一个包含图像地址的字符串(例如“@drawable/image”),并为LisView的元素设置图像。我已经创建了一个自定义SimpleCursorAdapter来更改字体。这是我的自定义SimpleCursorAdapter代码:

public class CustomFontExampleAdapter extends SimpleCursorAdapter 
{
    private Typeface mCustomFont;

    @SuppressWarnings("deprecation")
    public CustomFontExampleAdapter(final Context context, final int layout, final Cursor c, final String[] from,
                final int[] to) {
            super(context, layout, c, from, to);
        mCustomFont = Typeface.createFromAsset(context.getAssets(), "font/Pompiere-Regular.ttf");
    }

    @Override
    public void bindView(final View view, final Context context, final Cursor cursor) {
        super.bindView(view, context, cursor);

        final TextView _TextViewTitle = (TextView) view.findViewById(R.id.record_nome);     
        _TextViewTitle.setTypeface(mCustomFont);                          
    }

}
这是我设置适配器时的代码:

d= new Database(getActivity());

        final Cursor c = d.scelta();

        CursorLoader(c);

        String from[] = {Codice.DATI_ID,
                Codice.DATI_NOME_DIETA};

        int to[] = {R.id.record_id,
                R.id.record_nome};

        @SuppressWarnings("deprecation")
        CustomFontExampleAdapter sca = new CustomFontExampleAdapter(rootView.getContext(),
                R.layout.singolo_elemento,
                c, from, to);
        sceltadieta = (ListView) rootView.findViewById(R.id.scelta_dieta);
        sceltadieta.setAdapter(sca);
在这一行之后:

_TextViewTitle.setTypeface(mCustomFont);    
你应该去:

ImageView _ImageView = (ImageView) view.findViewById(R.id.your_image_id)

    //Suppose you've already get your string(because it's not part of a question)
Drawable drawable = getResources().getDrawable(getResources()
                  .getIdentifier(yourStringName, "drawable", context.getPackageName()));
_ImageView.setImageDrawable(drawable)

哦,据我所知,你的适配器中没有上下文,将上下文放到适配器中是很常见的事情,你应该通过它抛出构造函数(就像你做的那样),并存储对它的引用(创建字段并通过构造函数中的上下文初始化它)

对不起,尼克,你能解释一下如何获取字符串吗?你写了数据库吗?是的,我已经用每个图像的引用填充了它,这个字符串将是游标中当前项的图像名称的字段,仅此而已,您知道如何获取它吗?