Android:向 ExpandableListView 添加标头视图时出现 ClassCastException

发布于 2024-10-06 13:57:11 字数 960 浏览 10 评论 0原文

我正在尝试向 ExpandableListView 添加标头,如下所示:

headerView = View.inflate(this, R.layout.header, null);
expandableListView.addHeaderView(headerView);
expandableListView.setAdapter(new SectionedAdapter(this));

这给了我以下错误:

 12-08 16:23:42.354:
 ERROR/AndroidRuntime(421): Caused by:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
 12-08 16:23:42.354:   ERROR/AndroidRuntime(421): at android.widget.ListView.clearRecycledState(ListView.java:504)
 12-08 16:23:42.354: ERROR/AndroidRuntime(421): at android.widget.ListView.resetList(ListView.java:490)
 12-08 16:23:42.354:ERROR/AndroidRuntime(421):at android.widget.ListView.setAdapter(ListView.java:422)
 12-08 16:23:42.354:ERROR/AndroidRuntime(421): at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)

这是在调用 expandableListView.setAdapter(new SectionedAdapter(this)) 时发生的,但我可以'不明白为什么。有什么想法吗?

I'm trying to add a header to an ExpandableListView like so:

headerView = View.inflate(this, R.layout.header, null);
expandableListView.addHeaderView(headerView);
expandableListView.setAdapter(new SectionedAdapter(this));

Which gives me the following error:

 12-08 16:23:42.354:
 ERROR/AndroidRuntime(421): Caused by:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
 12-08 16:23:42.354:   ERROR/AndroidRuntime(421): at android.widget.ListView.clearRecycledState(ListView.java:504)
 12-08 16:23:42.354: ERROR/AndroidRuntime(421): at android.widget.ListView.resetList(ListView.java:490)
 12-08 16:23:42.354:ERROR/AndroidRuntime(421):at android.widget.ListView.setAdapter(ListView.java:422)
 12-08 16:23:42.354:ERROR/AndroidRuntime(421): at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)

This is happening at the call to expandableListView.setAdapter(new SectionedAdapter(this)), but I can't figure out why. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月隐月明月朦胧 2024-10-13 13:57:11

好吧,我想出了这个。我通过以编程方式将视图的 LayoutParams 设置为 ListView LayoutParams 摆脱了运行时错误,如下所示:

headerView.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT));

在添加视图之前。原因可以在 Android 文档中找到:

http: //developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)

其中指出:

这些提供参数
此视图的父级,指定应如何排列它。有许多
ViewGroup.LayoutParams 的子类,
这些对应于不同的
ViewGroup 的子类是
负责安排他们的
孩子们。

所以基本上,如果您将一个视图添加到另一个视图,则必须将视图的 LayoutParams 设置为父级使用的 LayoutParams 类型,否则您将收到运行时错误。

Ok, I figured this one out. I got rid of the runtime error by programatically setting the View's LayoutParams to ListView LayoutParams, like so:

headerView.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT));

before adding the view. The reason being is found in the Android docs:

http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)

which states that:

These supply parameters to the
parent of this view specifying how it should be arranged. There are many
subclasses of ViewGroup.LayoutParams,
and these correspond to the different
subclasses of ViewGroup that are
responsible for arranging their
children.

So basically, if you are adding a view to another, you MUST set the LayoutParams of the view to the LayoutParams type that the parent uses, or you will get a runtime error.

小猫一只 2024-10-13 13:57:11

尽管上述答案可能对许多人有用,但仍然有更好的解释。
ClassCastException 是调用 listview.addHeaderViewlistview.addFooterView 后的正常行为。

原因是初始适配器包装在 HeaderViewListAdapter 中。
要获取您的适配器,请使用此代码。

YourAdapter ya = (YourAdapter) ((HeaderViewListAdapter)lv.getAdapter()).getWrappedAdapter();

代码取自此处

Even though the above answer might work for many, there is still a better explanation.
ClassCastException is the normal behaviour after calling listview.addHeaderView or listview.addFooterView.

The reason is that the initial adapter is wrapped in the HeaderViewListAdapter.
To get your adapter use this code.

YourAdapter ya = (YourAdapter) ((HeaderViewListAdapter)lv.getAdapter()).getWrappedAdapter();

Code taken from here

无人问我粥可暖 2024-10-13 13:57:11

就我而言,这不起作用。在设置适配器之前,我必须将 footerView 设置为 listView。首先,我在 OnCreate 方法中从布局文件初始化了 loadingView:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
loadingView = layoutInflater.inflate(R.layout.loading_view, null);

然后我在同一方法中使用了此解决方法

this.setIsLoading(true);
listView.setAdapter(adapter);
this.setIsLoading(false);

private void setIsLoading(boolean isLoading)
{
    this.isLoading = isLoading;

    if (isLoading) {
        listView.addFooterView(loadingView);
    }
    else {
        listView.removeFooterView(loadingView);
    }
}

In my case this didn't work. I had to set a footerView to my listView before setting it's adapter. First I initialized my loadingView from a layout file in OnCreate method:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
loadingView = layoutInflater.inflate(R.layout.loading_view, null);

Then I used this workaround in the same method:

this.setIsLoading(true);
listView.setAdapter(adapter);
this.setIsLoading(false);

Where

private void setIsLoading(boolean isLoading)
{
    this.isLoading = isLoading;

    if (isLoading) {
        listView.addFooterView(loadingView);
    }
    else {
        listView.removeFooterView(loadingView);
    }
}
余生再见 2024-10-13 13:57:11

您应该将父视图放入您的膨胀视图中。

ListView listview = (ListView) findViewById(R.id.listview);
headerView = View.inflate(this, R.layout.header, listview, false); // set listview as parent view
listview.addHeaderView(headerview);

You should put parent view to your inflated view.

ListView listview = (ListView) findViewById(R.id.listview);
headerView = View.inflate(this, R.layout.header, listview, false); // set listview as parent view
listview.addHeaderView(headerview);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文