You'll need to override the SimpleAdapter
's getView(int position, View convertView, ViewGroup parent)
method and cast the result to a TextView
. That should be safe to do for the R.layout.list_item
layout, although you may want to double check that.
From there, setting a typeface works as usual.
Snippet, to be placed inside an (anonymous) extension of SimpleAdapter:
Typeface mTypeface = ... // only needs to be initialised once.
@Override public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textview = (TextView) view;
textview.setTypeface(mTypeface);
return textview;
}
If, perhaps at some point in the future, you're going to have a more complex layout made out of more than just a single TextView
, I'd consider implementing your own extension of ArrayAdapter
. There are heaps of examples on how to go about that (also look up the ViewHolder/RowWrapper pattern) and it'll give you full control.
Edit: example code below.
public class TypefacedSimpleAdapter extends SimpleAdapter {
private final Typeface mTypeface;
public TypefacedSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
mTypeface = Typeface.createFromAsset(context.getAssets(), /* typeface */);
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textview = (TextView) view;
textview.setTypeface(mTypeface);
return textview;
}
}
Copy-paste the class above and make sure to set up the type face field as per your requirements. Then replace the current SimpleAdapter
; i.e. something like this:
ListAdapter adapter = new TypefacedSimpleAdapter(AllNotes.this,
noteList, R.layout.list_item, new String[] {
KEY_NOTE_ID, KEY_NOTE_SUBJECT,
KEY_NOTE_DATE }, new int[] {
R.id.list_lbl_id, R.id.list_lbl_subject,
R.id.list_lbl_date }
);
Note that I didn't actually compile or run any of this. I'll leave it up to you to fill in any gaps and/or make corrections.