我正在用ImageAdapter extends BaseAdapter给agridview充气。gridview有两个textviews。我想为其中一个设置自定义字体。在Typeface font = Typeface.createFromAsset(getAssets(), "BABYCAKE.TTF");中使用ImageAdapter会产生错误The method getAssets() is undefined for the type ImageAdapter
ImageAdapter定义为

package com.amit.wozzle;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.LayoutInflater;

public class ImageAdapter extends BaseAdapter
{
private ArrayList<String> listCountry;
private ArrayList<String> scorestage;
private Activity activity;
Typeface font;

public ImageAdapter(Activity activity,ArrayList<String> listCountry, ArrayList<String> scorestage) {
    super();
    this.listCountry = listCountry;
    this.scorestage = scorestage;
    this.activity = activity;
    font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listCountry.size();
}

@Override
public String getItem(int position) {
    // TODO Auto-generated method stub
    return listCountry.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public static class ViewHolder
{
    public ImageView imgViewFlag;
    public TextView  txtViewTitle;
    public TextView  txtViewTitle2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder view;
    LayoutInflater inflator = activity.getLayoutInflater();

    if(convertView==null)
    {
        view = new ViewHolder();
        convertView = inflator.inflate(R.layout.grid_item, null);
        view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
        view.txtViewTitle2.setTypeface(font);
        view.txtViewTitle2 = (TextView) convertView.findViewById(R.id.textView2);

        // view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
        convertView.setTag(view);
    }
    else
    {
        view = (ViewHolder) convertView.getTag();
    }
    // view.txtViewTitle2.setBackgroundColor(Color.BLUE);
    view.txtViewTitle2.setText(listCountry.get(position));
    view.txtViewTitle.setText(scorestage.get(position));
    // view.imgViewFlag.setImageResource(scorestage.get(position));

    return convertView;
}
}

请帮忙。

最佳答案

尝试

Typeface font = Typeface.createFromAsset(activity.getAssets(), "BABYCAKE.TTF");

08-26 09:54