扩展BaseAdapter类中的字体错误 [英] Typeface error in extended BaseAdapter class
本文介绍了扩展BaseAdapter类中的字体错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用从BaseAdapter
扩展过来的ListViewAdapter class
tat,
I am using a ListViewAdapter class
tat extended from BaseAdapter
,
在尝试为三个textview设置自定义Typeface
时,在部署到模拟器后出现错误.
While trying to set a Custom Typeface
for three textviews getting error after deploy to emulator.
我可以将其设置为普通的Activity类,但是BaseAdapter
类中存在一些逻辑或语法问题,请指出我的代码中的错误,我们将不胜感激.
I can be able to set to a normal Activity class, but there is some logic or syntax problem in BaseAdapter
class, Point out the error in my code and it will be appreciated.
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
private Activity activity;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView rank;
TextView country;
TextView population;
ImageView flag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
rank = (TextView) itemView.findViewById(R.id.rank);
country = (TextView) itemView.findViewById(R.id.country);
population = (TextView) itemView.findViewById(R.id.population);
Typeface type = Typeface.createFromAsset(activity.getAssets(),"fonts/Paprika-Regular.ttf");
rank.setTypeface(type);
country.setTypeface(type);
population.setTypeface(type);
// Locate the ImageView in listview_item.xml
flag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set results to the TextViews
rank.setText(resultp.get(MainActivity.RANK));
country.setText(resultp.get(MainActivity.COUNTRY));
population.setText(resultp.get(MainActivity.POPULATION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), flag);
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.putExtra("rank", resultp.get(MainActivity.RANK));
// Pass all data country
intent.putExtra("country", resultp.get(MainActivity.COUNTRY));
// Pass all data population
intent.putExtra("population",resultp.get(MainActivity.POPULATION));
// Pass all data flag
intent.putExtra("flag", resultp.get(MainActivity.FLAG));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
}
推荐答案
您应该替换此
Typeface type = Typeface.createFromAsset(activity.getAssets(),
"fonts/Paprika-Regular.ttf");
使用
Typeface type = Typeface.createFromAsset(context.getAssets(),
"fonts/Paprika-Regular.ttf");
这篇关于扩展BaseAdapter类中的字体错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文