How to Get a Layout Inflater Given a Context?

AndroidListviewListadapter

Android Problem Overview


I am writing a custom implementation of a ListAdapter.

In its constructor, I'm taking in a Context, a resource ID (i.e. R.id.xxx representing the layout file), and a list and a map (these contain the data).

Now, the problem is that i will need a LayoutInflater to get the View object which is in the separate layout XML file.

How can I get hold of the LayoutInflater given only the Context?

Now, why I think this is possible, is that this is quite similar to what is being passed in to the constructor of an ArrayAdapter (context, resource, textViewResourceId, data array), and I figure the ArrayAdapter also has to make use of a LayoutInflater given only a Context.

But how can it be done?

Android Solutions


Solution 1 - Android

You can use the static from() method from the LayoutInflater class:

 LayoutInflater li = LayoutInflater.from(context);

Solution 2 - Android

You can also use this code to get LayoutInflater:

LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdwin LeeView Question on Stackoverflow
Solution 1 - AndroidDave WebbView Answer on Stackoverflow
Solution 2 - AndroidNguyenDatView Answer on Stackoverflow