I have three questions:
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?
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)
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;
};
};
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.
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.
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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With