将ListView添加到LinearLayout
我定义了一个XML布局。在运行时,我从xml布局文件中获取LinearLayout,我在运行时创建一个ListView。在ListView中运行时,我添加了许多RelativeLayout,如图5所示,这个RelativeLayout包含不同的其他UI组件,例如TextView、ImageView 等工作正常,但是当用户按下后退按钮时,我使用 bean 中的 setter 方法保存 ListView 对象,当用户再次进入活动恢复时,我从 getter 方法获取 ListView 并将其再次添加到我的 LinearLayout 中我在上面添加,但它给了我异常 java.lang.IllegalStateException: 指定的子项已经有父项。您必须首先在子级的父级上调用removeView()。在再次获取后添加ListView之前,我也尝试了这个MyLinearLayout.removeView(myListView)….但问题相同。 。任何建议...
I define an XML Layout .At run time I am get LinearLayout from xml Layout file ,I am creating a ListView at run.At run time in ListView I am adding many RelativeLayouts as shown in picture # 5 this RelativeLayout contains different other UI component like TextView,ImageView etc its working fine but when user press back button I am saving ListView object using setter method in beans,When user comes on activity Resume again I am getting ListView from getter method and adding it again to my LinearLayout as I add above but its giving me exception as java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.Before adding ListView after getting again I also try this MyLinearLayout.removeView(myListView)….but problem same . . Any suggestion…
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我现在了解,您的问题是,当您恢复活动时,您会从代码构建一个新的 ListView,而应用程序已经找到此 ListView。因此,您会收到一个错误,您首先需要将此 ListView 清空,然后用所有数据重新填充它。
最好的方法是:
但最好发布一些代码,以便我们可以更好地帮助您。
亲切的问候,祝你好运!
As I understand now, your problem is that when you resume the activity you build a new ListView from code while the app find already this ListView. So, you get an error that you first need the make this ListView empty and then repopulate it with all your data.
The best way to do this:
But it's maybe better to post some code, so we can better help you.
Kind regards and good luck!