列表视图中的数据更改事件应该在哪里处理?

发布于 2024-09-09 07:47:17 字数 381 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-09-16 07:47:17

我这样做是这样的:

  • 适配器具有用于底层数据操作的公共方法(例如,public void deleteItem(intposition)),它们完成其工作并调用notifyDataSetChanged( ) 最后;
  • Activity 在局部变量中保留对其 Adapter 的引用;
  • Listener 通常被创建为调用适当的 Adapter 方法的 Activity 的匿名内部类。

I do it this way:

  • Adapters have public methods for underlying data manipulation (e.g. public void deleteItem(int position)) that do their job and call notifyDataSetChanged() in the end;
  • Activity keeps a reference to it's Adapter in a local variable;
  • Listeners are usually created as anonymous inner classes of Activity calling appropriate Adapter methods.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文