1

I am trying to create a ListView that stores items in my own layout. I am following this tutorial to write my code. I am getting an exception within getView in my EventAdapter class, and it seems to come from assigning convertView. Everything I've seen on SO thus far says that this is the right way to assign convertView, so I'm at a loss for how to fix this.

Here is my custom ArrayAdapter:

public class EventAdapter extends ArrayAdapter<Event> {

    public EventAdapter(Context context, int eventListItemId, ArrayList<Event> eventList) {
        super(context, eventListItemId, eventList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // get the data of the event at this position
        Event event = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext())
                    .inflate(R.layout.event_list_item_layout, parent, false); 
            // ^^^^^^   The Exception is thrown above   ^^^^^^^
        }

        // get Textviews from event_list_item_layout
        TextView name = (TextView) convertView.findViewById(R.id.event_list_name);
        ...

        // set text on the TextViews
        name.setText(event.getName());
        ...

        return convertView;
    }
}

Here's the layout for event_list_item_layout, which is the child view:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:id="@+id/event_list_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        ...
    </ListView>

    ...
</LinearLayout>

This is the layout of the activity I want the list in (called search_results_activity):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/results_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/event_list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="@color/colorAccent"
        android:dividerHeight="1dp" />


</LinearLayout>

Here is the error log:

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
                      at android.widget.AdapterView.addView(AdapterView.java:478)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                      at com.mobiledevs.eventreminder.APIUtils.EventAdapter.getView(EventAdapter.java:38)

Anyone know where I'm going wrong?

Joseph Thweatt
  • 316
  • 2
  • 14
  • 1
    Hi you can read this post http://stackoverflow.com/questions/4576219/logcat-error-addviewview-layoutparams-is-not-supported-in-adapterview-in-a?rq=1, hopefully one of their answers work. – Fevly Pallar Jan 08 '17 at 02:55
  • I have actually looked at this before. I didn't see any solution that really helped me – Joseph Thweatt Jan 08 '17 at 19:10
  • Try this `LayoutInflater inflater = LayoutInflater.from(getContext());` then inside the *if (convertView == null){ .. }* block do `View aview = inflater.inflate(R.layout.event_list_item_layout, null, true);` – Fevly Pallar Jan 08 '17 at 19:49
  • That's not working either. The error is saying that addView() is being called by the `inflater.inflate()` line. Is there any way to prevent this from happening? – Joseph Thweatt Jan 08 '17 at 20:29
  • 1
    Try removed ListView from your event_list_item_layout,XML. Hope that help! – i_A_mok Jan 10 '17 at 02:27
  • Wow. That was it?! Thanks I_A_Mok, I've been scratching my head over this problem for a while now – Joseph Thweatt Jan 10 '17 at 04:24

0 Answers0