简体   繁体   English

使用SimpleCursorAdapter在ListView中设置图像

[英]Set image in a ListView using SimpleCursorAdapter

I want to set image in my ListView, my intention is to take a string contain the address of the image (ex. "@drawable/image") from database and to set the images for the element of the LisView. 我想在ListView中设置图像,我的意图是从数据库中获取包含图像地址(例如“ @ drawable / image”)的字符串,并为LisView的元素设置图像。 I have already create a custom SimpleCursorAdapter to change the font.This is my custom SimpleCursorAdapter code: 我已经创建了一个自定义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);                          
    }

}

This is my code when i set the adapter: 这是我设置适配器时的代码:

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);

after this line : 在这行之后:

_TextViewTitle.setTypeface(mCustomFont);    

Yous should go : 您应该去:

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)

Oh, and as far as i see you have no context in your adapter, putting context to adapter is common thing, you should past it throw constructor(as you did), and storing reference for it(create field and initialize it by context from constructor) 哦,据我所知,在适配器中没有上下文,将上下文放入适配器是很常见的事情,您应该过去将其抛出构造函数(如您所做的那样),并为它存储引用(创建字段并通过上下文从上下文初始化它)构造函数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SimpleCursorAdapter和ViewBinder在ListView项目中设置图像 - Setting an image in ListView item using SimpleCursorAdapter and ViewBinder 如何使用带有MediaStore的Custom SimpleCursorAdapter为ListView获取正确的图像? - How to get the correct image for the ListView using Custom SimpleCursorAdapter with MediaStore? 如何在Android中将simplecursoradapter设置为listview? - how to set the simplecursoradapter to a listview in android? 使用SimpleCursorAdapter将颜色设置为ListView Android - Set color to ListView Android with SimpleCursorAdapter Android-使用SimpleCursorAdapter动态ListView - Android - Dynamically ListView using SimpleCursorAdapter 使用 SimpleCursorAdapter OnItemClickListener 的 ListView 不起作用 - ListView using SimpleCursorAdapter OnItemClickListener not working 使用SimpleCursorAdapter在ListView中没有数据 - No data appearing in ListView, using SimpleCursorAdapter 使用SimpleCursorAdapter使用EditText搜索Listview - Search Listview with EditText using SimpleCursorAdapter 在 Android 中使用 SimpleCursorAdapter 填充列表视图 - Populate listview using SimpleCursorAdapter in Android 如何使用重写SimpleCursorAdapter将OnClickLitsener设置为ListView中的Button - How to set OnClickLitsener to Button in ListView with Override SimpleCursorAdapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM