如何在适配器的getView中获取listview的上下文

spr*_*t12 8 fonts android listview typeface layout-inflater

我有三个问题:

  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;
    };
};
Run Code Online (Sandbox Code Playgroud)

小智 11

public View getView(int position, View convertView, ViewGroup parent)
Run Code Online (Sandbox Code Playgroud)

ViweGroup父肯定不是null,因此,parent.getContext()可能是获取上下文的最佳方式