如何使用适配器将自定义字体设置为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;
}
}
答案 0 :(得分:0)
Typeface tf = Typeface.createFromAsset(convertView.getContext().getAssets(), "fonts/Molot.otf");
这可能就是原因。你不应该在适配器中从convertview获取上下文,即使它有一个。这种东西属于你的适配器的构造函数,它接受传递的活动的上下文。我重新格式化了你的构造函数。
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);
}
......