【问题标题】:retrieve original adapter from expandablelistview with header使用标题从可扩展列表视图中检索原始适配器
【发布时间】:2013-06-04 01:10:01
【问题描述】:

我正在使用带有扩展 BaseExpandableListAdapter 的自定义适配器的 ExpandableListView。 现在我想向该可扩展列表添加一个标题,因此我不需要使用我的 apapter 来获取元素的视图,而是使用标题“自动”创建的元素,所以我没有得到 IndexOutOfBoundsExceptions,因为现在标题是 0 位置的元素。

在我的适配器不可扩展的其他情况下,我只需要调用 myList.getAdapter() 并且一切正常,但现在我需要获取扩展 BaseExpandableListAdapter 的适配器,但我找不到这样做的方法。

myList = (ExpandableListView)findViewById(R.id.my_list);
myList .setGroupIndicator(null);
myList .setOnGroupClickListener(this);
myList .setOnChildClickListener(this);

//add a header
View addSummaryLayout = View.inflate(this, R.layout.add_summary, null); 
groupedMultimediaListView.addHeaderView(addSummaryLayout);  

myAdapter = new MyAdapter (this, uploadingMedias);
myList .setAdapter(myAdapter );

有时,我需要为 myAdapter 调用 getChildView 方法(扩展 BaseExpandableListAdapter),但我不能直接使用 myAdapter,因为它没有标题。我需要通过列表来完成。我正在尝试通过 getWrappedAdapter 来做到这一点

HeaderViewListAdapter headerAdapter = ((HeaderViewListAdapter) myList .getAdapter());
((MyAdapter ) headerAdapter.getWrappedAdapter()).getChildView(0, i - 1, false, view, myList );

但它给了我一个 ClassCastException,android.widget.ExpandableListConnector 不能转换为 MyAdapter

有人知道通过列表方法获取原始适配器的方法吗?

谢谢

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    来自docs

    public ListAdapter getAdapter ()
    
    Added in API level 1
    This method should not be used, use getExpandableListAdapter().
    

    所以使用 getExpandableListAdapter() 然后 getChildView

    【讨论】:

    • 谢谢!我没有看到警告并且正在使用另一个适配器让自己发疯!
    • 太棒了,我挠头很久了。在使用我的自定义函数add(CustomObject obj) 时,使用我的自定义适配器强制转换 getAdapter() 不会导致问题,但它并不令人耳目一新。拯救了我的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多