需要把ExpandableListView的适配器(继承自BaseExpandableListAdapter)同时绑定到AutoCompleteTextView,实现搜索过滤功能。实现了接口Filterable,以为成功在即,既然编译抛错误
The generic method setAdapter(T) of type AutoCompleteTextView is not applicable for the arguments (MyExpandableListAdapter). The inferred type MyExpandableListAdapter is not a valid substitute for the bounded parameter <T extends ListAdapter & Filterable>
MyExpandableListAdapter未实现ListAdapter,跟进BaseExpandableListAdapter最终继承的接口ExpandableListAdapter既然不是ListAdapter的子类!ExpandableListView继承自ListView google既然让它实现不同的适配器,哦!狗屎!
继续看看setAdapter有两个重载
public void setAdapter(ExpandableListAdapter adapter)
public void setAdapter(ListAdapter adapter)
如果调用
public void setAdapter(ListAdapter adapter)
直接就抛异常。看另外一个函数的实现
public void setAdapter(ExpandableListAdapter adapter) {
// Set member variable
mAdapter = adapter;
if (adapter != null) {
// Create the connector
mConnector = new ExpandableListConnector(adapter);
} else {
mConnector = null;
}
// Link the ListView (superclass) to the expandable list data through the connector
super.setAdapter(mConnector);
}
看,google既然在ExpandableListAdapter和ListAdapter直接做一个链接器来适配ListView。
ExpandableListView的这种实现方法,虽然可重用listview,但函数setAdapter签名和父类ListView不统一,无疑给调用者添加陷阱和增加复杂度。
回到上面的问题:如果需要MyExpandableListAdapter同时能够被AutoCompleteTextView做adapter,只好乖乖实现ListAdapter,所以要实现ListAdapter、ExpandableListAdapter两个接口。这么来编译还是不能通过。
For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of " +
"setAdapter(ListAdapter)
聪明的java编译器把MyExpandableListAdapter认为是ListAdapter的子类了。怎么办?很简单!一个小技巧让编译器更加聪明点
expandableListView.setAdapter((ExpandableListAdapter)MyExpandableListAdapter);
至此MyExpandableListAdapter就不仅可以被ExpandableListView使用而且可以被AutoCompleteTextView使用了。
希望此文对您有用!
分享到:
相关推荐
Android项目实战--手机卫士33--ExpandableListView的使用
android--ExpandableListView实例
Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友
本人博客:Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友列表的demo
换种思路解决列表套列表-ExpandableListView的使用,支持上拉加载,更改数据源
可以扩展的listview,类似于手机qq分组的listview(自己在网上找demo,找到的全是一些文档,好不容易把它拼成了一个demo,决定跟大家分享)
Android 时光轴 expandablelistview
Android学习笔记(二五): 多信息显示-ExpandableListView的使用
比LIStVIEw更好,适合初学者下载;很实用;LIStVIEw的升级
时间轴demo,可以后期根据需求 自己改动界面和bean数据结构。
ExpandableListView实现购物车页面.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
ExpandableListView简单实现商品列表折叠
CommentWithReplyView-master 基于ExpandableListView实现评论和回复的功能。 > 说明 ...处理了NestedScrollView、ExpandableListView和CoordinatorLayout的嵌套问题 点击某条评论,即可@ta进行回复
Android应用源码开发Demo,主要用于毕业设计学习。
Android应用源码开发Demo,主要用于毕业设计学习。
Android应用源码开发Demo,主要用于毕业设计学习。
基于ExpandableListView实现评论和回复的功能
expandablelistview-demo 带有ExpandableListView和CheckedTextView作为列表项的示例应用程序。
DragNDropExpandableListView 具有拖放功能的ExpandableListView。
ExpandableListView即listview的嵌套 ExpandableListView顾名思义,为可展开的ListView(相当于ListView的每一行都是一个可以点击并展开的ListView)