gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:58:23 25 4
gpt4 key购买 nike

如何使用适配器将自定义字体设置为 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;

}

}

最佳答案

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

这可能就是原因。即使适配器确实有上下文,您也不应该从适配器中的 ConvertView 获取上下文。这种事情属于你的适配器的构造函数,它采用传入的 Activity 的上下文。我也重新格式化了你的构造函数。

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);
}

......

关于c# - 如何使用适配器将自定义字体设置为 ListView 项中的 TextView 之一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13737264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com