I have to change the typeface on a custom list adapter. I've a problem, because i can't get the current context.

getassets() not exist for the class
getApplicationcontext()  not exist for the class
getBaseContext()  not exist for the class

i'm trying to get the context from the view, there're no error, but font not change

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Context  context = convertView.getContext();
//or
       Context  context2 = parent.getContext();

after in the code i write

 bold = Typeface.createFromAsset(context.getAssets(), "fonts/Jennifer-Lynne.ttf");
        italic = Typeface.createFromAsset(context.getAssets(), "fonts/helvetica-italic.ttf");
        holder.titoloView.setTypeface(bold);
        holder.autoreView.setTypeface(italic);

idea?

有帮助吗?

解决方案 2

this is the correct code (font on assets folder)

Global variable

Typeface tf, bold, italic;
   Context prova;

the metod where i set the context

public CopyOfCustomListAdapterLele(Context context, ArrayList listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
        prova = context;
    }

and the setting of the font

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;


        bold = Typeface.createFromAsset(prova.getAssets(), "fonts/Jennifer-Lynne.ttf");



        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.singolopunto, null);
            holder = new ViewHolder();
            holder.titoloView = (TextView) convertView.findViewById(R.id.textView1); //titolo canzone

            holder.imageView = (SmartImageView) convertView.findViewById(R.id.imageView2); //immagine
            holder.free = (ImageView) convertView.findViewById(R.id.imageView3); //immagine free

            holder.titoloView.setTypeface(bold);


...

其他提示

Pass a Context to your CustomListAdapter class constructor and use that to get your font from the assets. Set the font when you initialize your ViewHolder views (when convertView is null in getView).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top