1

I'm trying to inflate a layout in my getView method of my adapter:

Here is the error:

01-18 20:58:04.873: E/AndroidRuntime(21583): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-18 20:58:04.873: E/AndroidRuntime(21583):    at android.widget.AdapterView.addView(AdapterView.java:478)
01-18 20:58:04.873: E/AndroidRuntime(21583):    at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
01-18 20:58:04.873: E/AndroidRuntime(21583):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-18 20:58:04.873: E/AndroidRuntime(21583):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-18 20:58:04.873: E/AndroidRuntime(21583):    at com.myname.myapp.gui.MyAdapter.getView(MyAdapter.java:46)

The error (according to eclipse happens on the converView =mInflater...

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        convertView = mInflater.inflate(
                android.R.layout.simple_list_item_1, parent);
    }
    ((TextView) convertView.findViewById(R.id.text1)).setText(titles
            .get(position));
    return convertView;
}

Here is the the constructor to my adapter:

public MyAdapter(ArrayList<String> list, Context context) {
        titles = list;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }
EGHDK
  • 17,818
  • 45
  • 129
  • 204

3 Answers3

8

The best way to do this is

convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);

Yes, you should pass the actual parent because the layout that might affect the parameters used in the inflation. The third parameter, means whether the inflate method should add the view to the parent or not.

By default, it will add the view to the parent and in this case you want to return the view but not add it to the parent.

You can read this here:

http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean)

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters:

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.

Returns The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
3

Change this line:

convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent);

For this one:

convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
codeMagic
  • 44,549
  • 13
  • 77
  • 93
Typo
  • 1,875
  • 1
  • 19
  • 32
0

Use below like of code:

 LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.demolistlayout,parent,false);

Even though it is not fixed then you have to check your layout file. and mostly it has a problem with drawable images.

BlackBeard
  • 10,246
  • 7
  • 52
  • 62
amith singh
  • 53
  • 1
  • 4