bob*_*tko 38 listview header dataset adapter notify
我在listVivew的标题中添加了一个视图,
View TopSearch = (View) View.inflate(this, R.layout.search, null);
lv.addHeaderView(TopSearch, null, false);
Run Code Online (Sandbox Code Playgroud)
一切都很好,直到我尝试执行(数据更改时)
adapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)
总是崩溃我的应用程序给我以下错误:
> java.lang.ClassCastException:android.widget.HeaderViewListAdapter
如果我删除标题视图,则没有错误.有什么建议?谢谢.
Mar*_*zon 126
似乎无论何时在列表视图中使用页眉/页脚视图,ListView都会被HeaderViewListAdapter包装.您可以使用以下代码解决此问题:
((YourAdapter)((HeaderViewListAdapter)lv.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)
Neb*_*ebu 12
API 18及更低版本对它的包装感到困惑.为了帮助它,请在分配适配器之前设置页眉和/或页脚.这样,正确的包裹发生在封面下.然后立即删除页眉/页脚(如果这是你想要的).
myList.addFooterView(myFooterView);
myList.setAdapter(adapter);
myList.removeFooterView(myFooterView);
Run Code Online (Sandbox Code Playgroud)
如http://stanllysong.blogspot.ru/2013/08/javalangclasscastexception.html中所述 ,应该这样做:
HeaderViewListAdapter hlva = (HeaderViewListAdapter)l.getAdapter();
YourListAdapter postAdapter = (YourListAdapter) hlva.getWrappedAdapter();
postAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)
小智 -2
public class YourOwnList extends ListActivity {
private EfficientAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
mAdapter = new EfficientAdapter(/*your parameters for the adapter*/);
}
private void yourMethod () {
mAdapter.notifyDataSetChanged();
}
private static class EfficientAdapter extends CursorAdapter {
// your adapter
}
}
Run Code Online (Sandbox Code Playgroud)