getLayoutInflater() in fragment

AndroidAndroid Fragments

Android Problem Overview


I'm trying to show my results search in a ListView on the Fragment, but it fails on:

LayoutInflater inflater = getLayoutInflater();

> The method getLayoutInflater(Bundle) in the type Fragment is not > applicable for the arguments ()

This is my code:

public View getView(int position, View convertView, ViewGroup parent)
  {
   LayoutInflater inflater = getLayoutInflater();
   View row;

   row = inflater.inflate(R.layout.list_single, parent, false);

   TextView textview = (TextView) row.findViewById(R.id.TextView01);
   ImageView imageview = (ImageView) row
     .findViewById(R.id.ImageView01);

   textview.setText(data_text[position]);
   imageview.setImageResource(data_image[position]);

   return (row);

  }

How can I fix this?

Android Solutions


Solution 1 - Android

if you are in a fragment you want

getActivity().getLayoutInflater();

or

LayoutInflater.from(getActivity());

also you can do

View.inflate();

or

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Solution 2 - Android

In Activity you can use like this:

this.getLayoutInflater();

For Fragment you need to replace keyword "this" with "getActivity()"

getActivity().getLayoutInflater();

Hope this will help!

Solution 3 - Android

Now and don't know from when you can get it in your Fragment(support library v4) by

getLayoutInflater(null);

Solution 4 - Android

If you want to inflate layout (attach child layout to parent) in Fragment

Use this code

LayoutInflater inflater = (LayoutInflater) getActivity() .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Now to Inflate child layout

View view = inflater.inflate(R.layout.layout_child, null);

Happy coding :)

Solution 5 - Android

Use it:

LayoutInflater inflater = (LayoutInflater)getContext().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
QuestionudinsekomplekView Question on Stackoverflow
Solution 1 - AndroidtyczjView Answer on Stackoverflow
Solution 2 - Androidchan sopheapView Answer on Stackoverflow
Solution 3 - AndroidAhmed HegazyView Answer on Stackoverflow
Solution 4 - AndroidHamza MehmoodView Answer on Stackoverflow
Solution 5 - AndroidKRUPESHView Answer on Stackoverflow