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?