我有三个问题:
我使用的是 getApplicationContext,这与我看到的所有仅说明上下文的示例不同。我如何在这里获取上下文?或者应用上下文是否正常?
我覆盖 getView 而不是让它自己处理它是否有任何性能损失(我这样做只是为了设置自定义字体)
在使用这种方法时有什么我应该注意的吗(因为我只是复制和粘贴,但不了解如果我的列表中有 250 个项目会做什么)。我会导致任何潜在的泄漏吗?
我的代码:
private Typeface arabicFont;
arabicFont = Typeface.createFromAsset(getAssets(), "arabicfont.ttf");
...
_arabicAdapter = new SimpleCursorAdapter(this,
R.layout.book_list_item_arabic,
_cursor,
new String[] {"NumberArabic", "Arabic"},
new int[] {R.id.txtNumber, R.id.txtBookName},
CursorAdapter.NO_SELECTION)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.book_list_item_arabic, parent, false);
}
TextView txtBookName = (TextView)convertView.findViewById(R.id.txtBookName);
txtBookName.setTypeface(arabicFont);
txtBookName.setText("\"العربية\"");
return convertView;
};
};
最佳答案
public View getView(int position, View convertView, ViewGroup parent)
ViweGroup parent肯定不为null,所以parent.getContext()可能是获取context的最佳方式
关于android - 如何在 ListView 的适配器的getView中获取上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20012590/