android - 如何在 ListView 的适配器的getView中获取上下文

标签 android listview fonts layout-inflater typeface

我有三个问题:

  1. 我使用的是 getApplicationContext,这与我看到的所有仅说明上下文的示例不同。我如何在这里获取上下文?或者应用上下文是否正常?

  2. 我覆盖 getView 而不是让它自己处理它是否有任何性能损失(我这样做只是为了设置自定义字体)

  3. 在使用这种方法时有什么我应该注意的吗(因为我只是复制和粘贴,但不了解如果我的列表中有 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/

相关文章:

安卓服务 : How to start the Service

JavaFx View 列表与不同实例共享值

android - 为什么 notifyDataSetChanged() 没有更新我的列表

java - LibGDX True Type 字体在按钮中的使用

android - 如何以编程方式从 Google Play 下载 APK 文件?

android - 带有 Cordova 的 jsPDF - 添加图像

css - 尽管设置了相同的字体,但在 Qt 和 Blink 中呈现的文本看起来非常不同

css - 为什么 bootstrap 在 Sass 中有两种字体?

android - Firebase 分析 : "select_content" events with "Content_type" value: (not set)

在 firebase 删除数据时删除 Android ListView