Bug 41582 - ListView crashes if the view added in the adapter is created programatically, NOT inflated from layout
Summary: ListView crashes if the view added in the adapter is created programatically,...
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Bindings ()
Version: 6.0.1 (C6SR1)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Atsushi Eno
URL:
Depends on:
Blocks:
 
Reported: 2016-06-08 05:40 UTC by Ales Suchac
Modified: 2016-09-20 15:44 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Modified CustomRowView sample (112.77 KB, application/octet-stream)
2016-06-08 05:59 UTC, Ales Suchac
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Ales Suchac 2016-06-08 05:40:00 UTC

    
Comment 1 Ales Suchac 2016-06-08 05:59:36 UTC
Created attachment 16226 [details]
Modified CustomRowView sample

For your convenience, modified sample - crashes when trying to run
Comment 2 Ales Suchac 2016-06-08 06:04:50 UTC
Sorry, forgot to save description - here we go:

If you try to add to ListView a programatically created view, it crashes at runtime with the exception:

Java.Lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

Here is the offending code - file HomeScreenAdapter.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;

namespace CustomRowView {
public class HomeScreenAdapter : BaseAdapter<TableItem> {
    List<TableItem> items;
    Activity context;
    public HomeScreenAdapter(Activity context, List<TableItem> items)
        : base()
    {
        this.context = context;
        this.items = items;
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override TableItem this[int position]
    {
        get { return items[position]; }
    }
    public override int Count
    {
        get { return items.Count; }
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = items[position];

        View view = convertView;
		if (view == null) {
			view = new View (this.context);		// <<<<<<<<<<<<<<<< --------- Programatically created view causes runtime crash -------- <<<<<<
			view.LayoutParameters = new ViewGroup.LayoutParams (100, 100);
			view.SetBackgroundColor (Color.Yellow);
		}

		/*  // ORIGINAL CODE:

        if (view == null) // no view to re-use, create new
            view = context.LayoutInflater.Inflate(Resource.Layout.CustomView, null);
        view.FindViewById<TextView>(Resource.Id.Text1).Text = item.Heading;
        view.FindViewById<TextView>(Resource.Id.Text2).Text = item.SubHeading;
        view.FindViewById<ImageView>(Resource.Id.Image).SetImageResource(item.ImageResourceId);

        */

        return view;
    }
}
}
Comment 3 dean.ellis 2016-09-20 15:44:54 UTC
The Sample runs fine on the latest stable Release Xamarin.Android 7.0.
So I am closing as Fixed. Please reopen if this is still a problem.