Ali Yılmaz
Ali Yılmaz

Reputation: 101

getLayoutInflater cannot resolve

This code belongs my arrayadapter for custom spinner.I'm getting error of cannot resolve method on getLayoutInflater() method and I don't know why. Any help will be appreciated.

    public View getCustomView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater= getLayoutInflater();
    View mySpinner = inflater.inflate(R.layout.spinneritems, parent, false);

    TextView main_text = (TextView) mySpinner.findViewById(R.id.spinneritem);
    main_text.setText(spinnerValues[position]);
    return mySpinner;
}

}

Upvotes: 2

Views: 6355

Answers (3)

Yeuni
Yeuni

Reputation: 282

Try this one works for me.

LayoutInflater inflater= LayoutInflater.from(context);

Upvotes: 4

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

cannot resolve method on getLayoutInflater()

Android getLayoutInflater () retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on.

In here you are missing to set Context

Don't

  LayoutInflater inflater= getLayoutInflater();

Do

LayoutInflater inflater= Your_class_context.getLayoutInflater();

Upvotes: 2

Kristiyan Varbanov
Kristiyan Varbanov

Reputation: 2509

Try like this

  LayoutInflater inflater= getActivity().getLayoutInflater();

or

 LayoutInflater inflater= youContext.getLayoutInflater();

because method getLayoutInflater() belong to Activity and you must get it with getActivity() method or pass context into your adapter constructor for example :

public class YourAdapter extends ArrayAdapter {

    private Context mContext;
    private ArrayList<Strings> someList;

    public YourAdapter (Context context, ArrayList<String> someList) {
        super(context, R.layout.comment_item, someList);
        **this.mContext = context;**
        this.someList = someList;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = **mContext**.getLayoutInflater();
            rowView = inflater.inflate(R.layout.yourItem, null);

            rowView.setTag(viewHolder);
        }

        return rowView;
    }

}

and in your Activity

YourAdapter mAdapter = new YourAdapter ( **getActivity()**,someList);

Upvotes: 2

Related Questions