2

may i know the meaning of

 public View getView(int position, View view, ViewGroup parent){
            if (view ==null)
                view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
        }
  • why it contain 3 parameter, wht is the meaning of those three param
  • layout inflater it must work with view? why?

anyone can explain to me thank!

3

3 Answers 3

7

LayoutInflater has two relevant overloads of inflate:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root)

The second parameter is the view you want to refer as the root of the view you're inflating. If attachToRoot is true, it will become the inflated view's parent view. If not, it'll just help create the correct layout.

If you use the second overload, attachToRoot is assumed to be true and if you supply the (optional) root, it'll be automatically attached as a parent view to the inflated view.

2

What's wrong with the documentation? http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int, android.view.ViewGroup, boolean)

2

The official definition of the method inflate comes in this way:

inflate(int resource, ViewGroup root, boolean attachToRoot)

Quoting we obtain the following concepts:

resource: ID for an XML layout resource to load (e.g., R.layout.main_page)

root: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)

attachToRoot: Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

For more information http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int, android.view.ViewGroup, boolean)

1
  • FWIW, R.layout.listview_item is not an XmlPullParser. There's another overload that takes an int as the first arg.
    – laalto
    Jun 7, 2014 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.