1

如何使用适配器将自定义字体设置为 ListView 项中的 TextView 之一?

我使用了这段代码,但在getView方法中出现运行时异常。

import java.util.ArrayList;

import java.util.HashMap;

import android.view.ViewGroup;

import com.hands.daily.duas.database.MyDbClass;

import android.app.Activity;

import android.content.Context;

import android.graphics.Typeface;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.BaseAdapter;

import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

    static int i=11;

    private Activity activity;

    public ArrayList<HashMap<String, String>> data;

    private static LayoutInflater inflater=null;

    First fst=new First();

  //  public ImageLoader imageLoader;

    String urlString="";

   MyDbClass mdbclass;

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> list) {

         data=new ArrayList<HashMap<String, String>>();

        activity = a;

        data=list;

        inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //  imageLoader=new ImageLoader(activity.getApplicationContext());

    }
    public int getCount() {

        return data.size();

    }

    public Object getItem(int position) {

        return position;

    }

    public long getItemId(int position) {

        return position;

    }

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

        View vi=convertView;

        if(convertView==null)

            vi = inflater.inflate(R.layout.duas_rowlist, null);

        TextView duatv = (TextView)vi.findViewById(R.id.textView1); // duatv

        Typeface tf = Typeface.createFromAsset(convertView.getContext().getAssets(),"fonts/Molot.otf");

        duatv.setTypeface(tf);

        HashMap<String, String> song = new HashMap<String, String>();

        song = data.get(position);

        // Setting all values in listview

       // String eventName1=song.get(First.Duas).toString();

        duatv.setTextSize(i);

     duatv.setText(song.get(First.Duas));

        return vi;

    }

}
4

1 回答 1

0
        Typeface tf = Typeface.createFromAsset(convertView.getContext().getAssets(), "fonts/Molot.otf");

这可能就是为什么。即使适配器确实有上下文,您也不应该从适配器中的转换视图中获取上下文。这种事情属于你的适配器的构造函数,它接受传入的活动的上下文。我也重新格式化了你的构造函数。

public ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater;
private Typeface tf;

public LazyAdapter(Context context, ArrayList<HashMap<String, String>> data) {
    this.data = data;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    tf = Typeface.createFromAsset(context.getAssets(), "fonts/Molot.otf");
    // imageLoader=new ImageLoader(context);
}

......
于 2012-12-06T06:33:22.920 回答