Android:用新数据填充 Activity 的最佳方式

发布于 2024-12-11 17:05:07 字数 454 浏览 0 评论 0原文

我遇到了以下“问题”。

我想要进行一项活动,向我显示某些球队在特定比赛日的排名。

因此我会添加一个微调器和一个 TableLayout。第一次开始时,活动应该显示实际比赛日的排名,但随后您可以选择任何其他同性恋日,排名应该会更新。

创建此活动的最佳方式是什么?

  1. 将整个 TableLayout 与所有 TableRows 和 TextView 组装在一起,给它们 id 并在运行时通过 id 更新这些视图。问题:巨大的不灵活的硬编码layout.xml

  2. 在运行时组装布局,添加ids,通过ids更新

  3. 在运行时组装布局。更新时删除旧视图并创建新视图

  4. 在运行时组装布局。更新时重新启动活动

只是想知道哪一个是最好的。或者有没有其他方法可以实现这个目标,

谢谢猎豹

i got the following "problem".

I want to have an activity thats shows me the standings of some teams at a specific gameday.

therefor i would add a spinner and a TableLayout. At the first Start the activity should show the standings of the actual gameday but then you can choose any other gaymeday and the standing should get updated.

Whats the best way to create this activity?

  1. assemble the whole TableLayout with all TableRows and TextViews, give them ids and update those views via id during runtime. Problem: huge unflexible hardcoded layout.xml

  2. assemble the layout during runtime, add ids, update via ids

  3. assemble the layout during runtime. on update remove old views and create new ones

  4. assemble the layout during runtime. on update restart the activity

just whant to know which one is the best. or is there any other way to achieve that

thx Cheetah

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦罢 2024-12-18 17:05:07

如果我是你,我实际上会使用带有适配器的 GridView。这将抽象掉所有布局更改的处理。您只需担心将数据映射到适当的视图即可。这个 示例 将 ImageView 映射到 GridView,但没有理由你无法映射到 GridView 中包含数据的 TextView。此外,由于您使用的是适配器,因此您可以利用所有 Loader< /a> 类,它们具有异步加载功能。

此外,使用该方法将使您的程序能够轻松适应数据集的变化。您将来可能希望向表中添加更多数据,这种方法可以让您轻松地做到这一点,而无需不断更改 xml 布局。

If I were you, I'd actually use a GridView with an Adapter. This will abstract away all the handling of layout changes. You just have to worry about mapping your data to appropriate views. This example maps ImageViews to a GridView, but there's no reason you couldn't map to TextViews containing your data in a GridView. Also, because you're using an adapter, you can take advantage of all the Loader classes and they're asynchronous loading capabilities.

In addition, using the approach will allow you program to easily adapt as your dataset changes. You may want to add more data to the table in the future and this approach will allow you to easily do that without having to constantly change your xml layouts.

旧情勿念 2024-12-18 17:05:07

浏览量有变化吗?如果没有。最好的方法是使用已经存在的视图并更新它们的值。尽量避免重新创建/重新膨胀视图,因为这是一项昂贵的任务。

Does the number of views change? If no. Best way is to use the already existent views and update their values. Try to avoid recreating/reinflating views since that's an expensive task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文