Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get context in getView of adapter for listview

I have three questions:

  1. I am using getApplicationContext unlike all the examples I have seen which just say context. How do I get the context here? Or is the application context fine?

  2. Is there any performance penalty for me overriding the getView instead of letting it handle it by itself (I am only doing it to set a custom font)

  3. Is there anything I should be aware of while using this approach (as I am just copy and pasting without understanding what it will do if I have 250 items in my list). Any potential leaks I can cause?

My Code:

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;
    };
};
like image 911
sprocket12 Avatar asked Nov 15 '13 23:11

sprocket12


People also ask

How do you connect an adapter with ListView write down the codes for it?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.

What is ListView and base adapter why we use base adapter?

BaseAdapter is a common base class of a general implementation of an Adapter that can be used in ListView, GridView, Spinner etc. Whenever you need a customized list in a ListView or customized grids in a GridView you create your own adapter and extend base adapter in that.

Which function is automatically called when the list item view is ready to be displayed?

The ListView instance calls the getView() method on the adapter for each data element. In this method the adapter creates the row layout and maps the data to the views in the layout. This root of the layout is typically a ViewGroup (layout manager) and contains several other views, e.g., an ImageView and a TextView .


1 Answers

public View getView(int position, View convertView, ViewGroup parent)

ViweGroup parent is certainly not null,so, parent.getContext() maybe the best way for fetch context

like image 109
sky Avatar answered Oct 17 '22 07:10

sky