我有一个例外我从未想过会看到.从ListView(原文如此)中删除页脚视图时适配器的类强制转换异常.
java.lang.ClassCastException: com.test.MyAdapter
at android.widget.ListView.removeFooterView(ListView.java:381)
Run Code Online (Sandbox Code Playgroud)
怎么会发生这种情况?删除页脚与类强制转换异常有什么关系????
该列表是一个多列表适配器,也许这就是为什么但仍然是一个用于删除页脚(sic)的类强制转换异常.
Vis*_*yas 12
ListView在调用setAdapter()方法之前添加页脚视图.
添加:
public void addFooterView (View v)
Run Code Online (Sandbox Code Playgroud)
从以下版本开始:API Level 1添加固定视图以显示在列表底部.如果多次调用addFooterView,则视图将按添加顺序显示.使用此调用添加的视图可以在需要时获得焦点.
注意:在调用setAdapter之前调用此方法.这样,ListView可以将提供的光标包装起来,也可以考虑页眉和页脚视图.
参数v要添加的视图.
你也可以查看这篇有趣的帖子.
希望这可以帮助.
这是上面答案的一些代码,它适用于我的情况:
在设置它的适配器之前,我必须在我的listView中设置一个footerView(它是一个带有分页的listView中的loadingView),然后将其删除.首先,我在OnCreate方法中从布局文件初始化了我的loadingView:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
loadingView = layoutInflater.inflate(R.layout.loading_view, null);
Run Code Online (Sandbox Code Playgroud)
然后我在同一个方法中使用了这个解决方法:
this.setIsLoading(true);
listView.setAdapter(adapter);
this.setIsLoading(false);
Run Code Online (Sandbox Code Playgroud)
哪里
private void setIsLoading(boolean isLoading)
{
this.isLoading = isLoading;
if (isLoading) {
listView.addFooterView(loadingView);
}
else {
listView.removeFooterView(loadingView);
}
}
Run Code Online (Sandbox Code Playgroud)