user5749218
user5749218

Reputation: 33

How to fix 'Cannot implicitly convert type BaseExpandableListAdapter to Android.Widget.IListAdapter'

I'm trying to set up a custom expandable list view adapter (inheriting from BaseExpandableListAdapter) to be used as an adapter for an ExpandableListView widget in Xamarin.Android.

I'm using Visual Studio 2019 and Xamarin.Android v28.0.0.1

Following examples from these websites I've got something that I believe should be working.

http://www.appliedcodelog.com/2016/06/expandablelistview-in-xamarin-android.html https://gist.github.com/lprichar/904ade6f1aaf96d172436bed3fb24d9b https://www.codeproject.com/Articles/677206/MonoAndroid-Writing-ExpandableListView-amd

answers_fragment.axml

<?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">
    <ExpandableListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/answers" />
</LinearLayout>

Fragments.cs

    public class AnswersFragment : Fragment
    {
        private readonly Dictionary<string, bool> answers;

        public AnswersFragment(Dictionary<string, bool> answers)
        {
            this.answers = answers;
        }

        // Display list of the questions and where answers matched.
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.Inflate(Resource.Layout.answers_fragment, container, false);
            view.FindViewById<ExpandableListView>(Resource.Id.answers).Adapter = new AnswersListViewAdapter(Context, answers); // This is where the implicit type conversion error occurs
            return view;
        }
    }

Adapter.cs

    class AnswersListViewAdapter : BaseExpandableListAdapter
    {
        private readonly Context context;
        private readonly Dictionary<string, bool> answers;

        public AnswersListViewAdapter(Context context, Dictionary<string, bool> answers)
        {
            this.context = context;
            this.answers = answers;
        }

        public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
        {
            return answers.Values.ElementAt(childPosition);            
        }

        public override long GetChildId(int groupPosition, int childPosition)
        {
            return childPosition;
        }

        public override int GetChildrenCount(int groupPosition)
        {
            return answers.Count;
        }

        public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
        {
            // Setup view logic
        }

        public override Java.Lang.Object GetGroup(int groupPosition)
        {
            return answers.Keys.ElementAt(groupPosition);
        }

        public override int GroupCount
        {
            get
            {
                return answers.Count;
            }
        }

        public override long GetGroupId(int groupPosition)
        {
            return groupPosition;
        }

        public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {
            // Setup view logic
        }

        public override bool HasStableIds
        {
            get
            {
                return false;
            }
        }
        public override bool IsChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }
    }

However, when I try to implement my custom adapter I get the following error:

Cannot implicitly convert type 'AnswersListViewAdapter' to 'Android.Widget.IListViewAdapter. An explicit conversion exists (are you missing a cast?)'

I thought it would be as simple as adding an (IListAdapter) cast in front of my adapter declaration but this throws the following error at runtime.

Unhandled Exception:

System.InvalidCastException: <Timeout exceeded getting exception details>

Can anyone see where I'm going wrong?

Upvotes: 1

Views: 427

Answers (1)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13919

For ease of understanding, we can define variables like this:

  AnswersListViewAdapter listAdapter;
  ExpandableListView expListView;

And use like this:

 public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        View view = inflater.Inflate(Resource.Layout.answers_fragment, container, false);
        expListView = view.FindViewById<ExpandableListView>(Resource.Id.answers);
        listAdapter = new AnswersListViewAdapter(Application.Context, answers); // This is where the implicit type conversion error occurs
        expListView.SetAdapter(listAdapter);

        //view.FindViewById<ExpandableListView>(Resource.Id.answers).Adapter = new AnswersListViewAdapter(Context, answers);

        return view;
    }

of course , you can also use directly like this :

 view.FindViewById<ExpandableListView>(Resource.Id.answers).SetAdapter(new AnswersListViewAdapter(Application.Context, answers));

Upvotes: 1

Related Questions