getLayoutInflater() in fragment

2019-01-16 12:03发布

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?

5条回答
可以哭但决不认输i
2楼-- · 2019-01-16 12:48

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

getLayoutInflater(null);
查看更多
Anthone
3楼-- · 2019-01-16 12:56

Use it:

LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看更多
做自己的国王
4楼-- · 2019-01-16 12:57

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);
查看更多
不美不萌又怎样
5楼-- · 2019-01-16 12:59

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!

查看更多
你好瞎i
6楼-- · 2019-01-16 13:00

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 :)

查看更多
登录 后发表回答