如何修复“无法将类型 BaseExpandableListAdapter 隐式转换为

我正在尝试设置一个自定义可扩展列表视图适配器(继承自 BaseExpandableListAdapter)以用作 Xamarin.Android 中 ExpandableListView 小部件的适配器。


我正在使用 Visual Studio 2019 和 Xamarin.Android v28.0.0.1


根据这些网站的示例,我得到了一些我认为应该有效的东西。


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>

片段.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;

        }

    }

一只萌萌小番薯
浏览 109回答 1
1回答

绝地无双

为了便于理解,我们可以这样定义变量:&nbsp; AnswersListViewAdapter listAdapter;&nbsp; ExpandableListView expListView;并像这样使用:&nbsp;public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.Inflate(Resource.Layout.answers_fragment, container, false);&nbsp; &nbsp; &nbsp; &nbsp; expListView = view.FindViewById<ExpandableListView>(Resource.Id.answers);&nbsp; &nbsp; &nbsp; &nbsp; listAdapter = new AnswersListViewAdapter(Application.Context, answers); // This is where the implicit type conversion error occurs&nbsp; &nbsp; &nbsp; &nbsp; expListView.SetAdapter(listAdapter);&nbsp; &nbsp; &nbsp; &nbsp; //view.FindViewById<ExpandableListView>(Resource.Id.answers).Adapter = new AnswersListViewAdapter(Context, answers);&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }当然你也可以直接这样使用:&nbsp;view.FindViewById<ExpandableListView>(Resource.Id.answers).SetAdapter(new AnswersListViewAdapter(Application.Context, answers));
打开App,查看更多内容
随时随地看视频慕课网APP