GetLayoutInflater permission error

When compiling a program with the code below, an error occurs. he says getLayoutInflater () is undefined. Can someone help me solve it as soon as possible.

 final LayoutInflater inflater = getLayoutInflater ( ); 

Thanks in advance.

+3
source share
4 answers

HI Prasanth, you can try this

Step 1. Create an object for the Inflater layout, as shown below:

LayoutInflater mInflater;

Step2: Analyze by passing context

Context context=MyActivity.getApplicationContext();
mInflater = LayoutInflater.from(context);

Step 3: In the meythod view mode, you can see the view as

public View getView(int arg0, View myView, ViewGroup parent) {


            if (convertView == null) {

myView = mInflater.inflate(R.layout.mytest, null);
}
+18
source

Afaik getLayoutInflater () is the method provided by Context. If this line is in a class that does not inherit from Context, this will not work. Pass the Context object and call the method on this object.

: , api docs, Activity

+1

This is what works for me: In my activity code, I pass the context as a parameter to my adapter as follows:

CustumAdapter myAdapter = new CustumAdapter(this, Items);

then I can use it in my adapter as follows:

public class CustumAdapter extends BaseAdapter {

ArrayList<ListItem> Items ;
public Context context;

public CustumAdapter(Context context, ArrayList<ListItem> Items) {
    this.Items = Items;
    this.context = context;
}
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {        
    LayoutInflater myInflater = LayoutInflater.from(context);
    View myview = myInflater.inflate(R.layout.row_item,null);

Everything is working fine :)

+1
source
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
0
source

Source: https://habr.com/ru/post/1793576/


All Articles