Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getLayoutInflater() in fragment

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?

like image 890
udinsekomplek Avatar asked Oct 14 '14 18:10

udinsekomplek


People also ask

What is an android Fragment?

According to the Android documentation, a fragment is a part of applications user interface that is bound to an activity. Fragments have their lifecycle and layouts or UI components. Fragments help enrich your UI design, pass data between different screens, and adapt to different device configurations.

What is LayoutInflater Android?

LayoutInflater is a class used to instantiate layout XML file into its corresponding view objects which can be used in Java programs. In simple terms, there are two ways to create UI in android. One is a static way and another is dynamic or programmatically.


2 Answers

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); 
like image 189
tyczj Avatar answered Oct 23 '22 11:10

tyczj


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!

like image 33
chan sopheap Avatar answered Oct 23 '22 12:10

chan sopheap