在`ImageAdapter中设置自定义字体扩展了BaseAdapter`

时间:2012-08-06 09:22:55

标签: android gridview fonts adapter

我正在使用ImageAdapter extends BaseAdapter来充气gridview。 Gridview有两个textview个。我想为其中一个设置自定义字体。在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;  
}  
}

请帮忙。

3 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:1)

为什么要在Typeface内创建getView个对象。 Typeface占用大量内存,由于getView内创建的字体对象数量较少,因此可用内存空间减少会降低应用速度。

而是应该只创建一次字体文件,并在需要时重复使用。在getView之外创建字体文件。如果仅在适配器内部使用它并在适配器构造函数中初始化它,则将其声明为适配器内的实例变量。而不是每次在getView中使用该单个实例设置字体时创建新实例。

对于您的错误,请使用活动实例变量来调用getAssests()

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

修改 - 试着像这样使用它 -

class DemoFonts{
    private static TypeFace typeFace;   
    public static TypeFace getTypeFace(Context mContext){
        if(typeface==null){
            typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/BABYCAKE.TTF");
        }

        return typeface;
    }

}

尝试使用如上所述。假设您在资产文件夹中有fonts个文件夹。

答案 2 :(得分:1)

view.txtViewTitle2给出空指针异常,因为您在初始化之前尝试访问textView。以下更改应该正常工作

view.txtViewTitle2 = (TextView) convertView.findViewById(R.id.textView2)
view.txtViewTitle2.setTypeface(font);