【问题标题】:Setting Adapter to ExpendableListView type mismatch将 Adapter 设置为 ExpendableListView 类型不匹配
【发布时间】:2020-01-02 17:31:15
【问题描述】:

当我尝试将适配器设置为我的 ExpendableListView 时,它需要 ListAdapter,但我想使用扩展 BaseExpandableListAdapter 的自定义 ExpandableListAdapter

这是我的 ExpandableListView:

<ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:layout_marginTop="160dp"
    android:id="@+id/exp_lv_nav">
</ExpandableListView>

设置适配器:

exp_lv_nav.adapter = ExpandableListAdapter(this, listDataHeader, listDataChild, exp_lv_nav)

这就是我在构建时得到的:

Error : Type mismatch: inferred type is ExpandableListAdapter but ListAdapter! was expected

我的适配器顶部:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<ExpandedMenuModel> mListDataHeader; 

    private HashMap<ExpandedMenuModel, List<String>> mListDataChild;
    ExpandableListView expandList;

    public ExpandableListAdapter(Context context, List<ExpandedMenuModel> listDataHeader, HashMap<ExpandedMenuModel, List<String>> listChildData, ExpandableListView mView) {
        this.mContext = context;
        this.mListDataHeader = listDataHeader;
        this.mListDataChild = listChildData;
        this.expandList = mView;
    }

【问题讨论】:

  • exp_lv_nav.adapter = ExpandableListAdapter(this, listDataHeader, listDataChild, exp_lv_nav) 嗯,它看起来不像 java ... 如果是 Kotlin ... 那么 ... 好吧 .... setAdapter(ListAdapter adapter) 和 setAdapter(ExpandableListAdapter adapter) fu** * 向上 Kotlin 绑定
  • 确实是 Kotlin
  • ...所以你试过直接使用setter吗?
  • kotlin 设置适配器的方式不适用于自定义适配器。只是WTF

标签: android kotlin android-listview expandablelistview listadapter


【解决方案1】:

这似乎是 Kotlin 的一个问题。现在,不要像问题中那样使用 Kotlin 生成的 getter/setter 设置适配器:

exp_lv_nav.adapter = ExpandableListAdapter(this, listDataHeader, listDataChild, exp_lv_nav)

替换为:

exp_lv_nav.setAdapter(ExpandableListAdapter(this, listDataHeader, listDataChild, exp_lv_nav))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 2022-12-05
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多