列表视图中的数据更改事件应该在哪里处理?
我有一个 ListView,它从我的适配器中填充了我的自定义视图。每个视图都有两个按钮,一个用于启动另一个活动来编辑该列表项的内容,另一个用于删除该项目。
我的问题是这些按钮的 ClickEvent 处理程序应该在哪里?既然我已经拥有了所需的所有信息,我是否应该将它们放在我的自定义视图代码中?我应该启动一个 ASyncTask 来删除该项目并更新适配器数据并调用 onDataSetChanged() 等吗?
所有这些都应该通过事件冒泡到我的 ListActivity 中吗?
我可能可以让它在任何级别上工作,但是沿着这个层次结构(Activity->ListView->Adapter->ListItemView)的哪个级别是编辑/删除 ListView 的支持数据的正确位置?
I have a ListView that is filled from my adapter with my custom views. Each view has two buttons, one that starts another activity to edit the contents of that list item and one to delete that item.
My question is where should my ClickEvent handlers for those buttons be? Should I put them right in my custom view code since I have all the information I need? Should I start an ASyncTask that deletes the item and updates the adapter data and calls onDataSetChanged() etc?
Should all of this be bubbled up through events to my ListActivity?
I could probably make it work at any level, but at what level along this hierarchy (Activity->ListView->Adapter->ListItemView) is the proper place to edit/delete backing data for the ListView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我这样做是这样的:
Activity
在局部变量中保留对其Adapter
的引用;Listener
通常被创建为调用适当的Adapter
方法的Activity
的匿名内部类。I do it this way:
Adapter
s have public methods for underlying data manipulation (e.g.public void deleteItem(int position)
) that do their job and callnotifyDataSetChanged()
in the end;Activity
keeps a reference to it'sAdapter
in a local variable;Listener
s are usually created as anonymous inner classes ofActivity
calling appropriateAdapter
methods.