您的位置:首页 > 其它

Cannot add header view to list -- setAdapter has already been called

2016-10-12 14:10 337 查看
在最近的新项目中我使用了Listview,在 Listview上我添加了很多view。

代码如下:

View view = inflater.inflate(R.layout.community, container, false);

listview = (ListView) view.findViewById(R.id.listview);

shequtitle = (TextView) view.findViewById(R.id.shequtitle);

v1 = LayoutInflater.from(context).inflate(R.layout.pagers, null);

v2 = LayoutInflater.from(context).inflate(R.layout.communitybtns, null);

v3 = LayoutInflater.from(context).inflate(R.layout.tebietuijian, null);

v4 = LayoutInflater.from(context).inflate(R.layout.kongbai, null);

viewpager = (ViewPager) v1.findViewById(R.id.viewpagers);

btn1 = (LinearLayout) v2.findViewById(R.id.btn1);

btn2 = (LinearLayout) v2.findViewById(R.id.btn2);

btn3 = (LinearLayout) v2.findViewById(R.id.btn3);

btn4 = (LinearLayout) v2.findViewById(R.id.btn4);

btn5 = (LinearLayout) v2.findViewById(R.id.btn5);

btn6 = (LinearLayout) v2.findViewById(R.id.btn6);

btn7 = (LinearLayout) v2.findViewById(R.id.btn7);

btn8 = (LinearLayout) v2.findViewById(R.id.btn8);

btn9 = (LinearLayout) v2.findViewById(R.id.btn9);

btn10 = (LinearLayout) v2.findViewById(R.id.btn10);

……………………

此处就是出现问题的地方,(错误代码)

*listview.setAdapter(new CommunityFragmentlistviewAdapter(context, list));

listview.addHeaderView(v1);

listview.addHeaderView(v2);

listview.addHeaderView(v3);*

根据问题的提示,就是我在addheaderview()这个方法应该在listview的setAdapter()方法之前。

正确代码是:

listview.addHeaderView(v1);

listview.addHeaderView(v2);

listview.addHeaderView(v3);

listview.setAdapter(new CommunityFragmentlistviewAdapter(

context, list));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐