我想在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);
答案 0 :(得分:0)
这一行之后:
_TextViewTitle.setTypeface(mCustomFont);
Yous应该去:
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)
哦,据我所知你的适配器中没有上下文,将上下文放到适配器是常见的事情,你应该通过它抛出构造函数(就像你做的那样),并为它存储引用(创建字段并初始化它)来自构造函数的上下文)