Android:一个 Activity 中有多个 ListView - 只有一个会更新图形

发布于 2024-12-26 15:34:03 字数 1665 浏览 0 评论 0原文

我已经被这个问题困扰了几天,但我自己似乎确实找不到答案。

我正在为 Android 设备构建一个应用程序。该应用程序将使用套接字连接到运行软件的 PC。用户可以使用一些按钮和东西来更改 PC 程序的状态 - 这部分效果很好。基本上是通过使用套接字向 PC 发送文本。

我还希望 Android 应用程序能够处理来自 PC 的传入事件。这些事件应该改变设备上的 UI,以反映 PC 程序的状态。

为了理解我的问题,我将尝试解释应用程序的结构。 基本上,该应用程序有四个活动:

  • 启动活动,显示启动屏幕,并在设定时间后启动下一个活动
  • 连接器活动,其中有一个输入,用户可以在其中输入 PC 的 IP。当设备连接到PC时,PC会向设备发送一个xml文件,基本包含程序的状态。解析此 xml 文件,当解析无错误时,此活动将启动下一个活动。
  • 下一个活动是 TabActivity,根据解析的 xml 创建 1 到 5 个选项卡。每个选项卡都需要一个活动来处理每个选项卡的内容。在这里,我选择对所有选项卡使用一项活动。
  • 最后一个活动是内容活动。这是一个列表活动。解析 xml 时,将创建项目并将其分为 5 个类别 - 每个类别一个选项卡。当第一次选择选项卡时,将生成内容,并为该选项卡创建一个 ListView 对象。对于类别中的每个项目,我都会膨胀布局 (xml) 并根据对象的状态设置正确的图形。这很好用。在选项卡之间切换也很好。

因此,当切换选项卡时,我检查选项卡的 ListView 是否为空,如果是这样,我将创建内容,如果不是,那么我将使用正确的“list.setListView”调用“setListAdapter(...)”。 getAdapter()”,其中 list 将是与选项卡相关的列表。

当从 PC 接收到事件时,我有一个处理程序来处理该事件。该处理程序将解析该消息,并找出要更改 UI 的哪一部分,以及与该 UI 部分相关的对象。然后,对象的状态会发生任何变化 - 如果对象的选项卡当前打开,视图也会更改 - 如果打开另一个视图,布尔值将告诉选项卡中的对象已更改状态。

我尝试从 ListView 变量获取视图并更改子元素,但是当列表不是当前列表时,这不起作用。

然后,当按下选项卡时,我尝试为该选项卡创建一个全新的 ListView,如果此布尔值显示某些内容已更改(就像第一次创建它一样) - 这只适用于一个选项卡。

我尝试使用两个选项卡(2 和 3)对其进行测试。该应用程序在选项卡 1 上启动。如果我随后打开选项卡 2,然后打开选项卡 3,则从两个选项卡向 PC 发送命令,一切正常。当 PC 向应用程序发送事件时,只有选项卡 3 会实际更新。在选项卡处于活动状态和非活动状态时都可以工作(活动时更新)。而选项卡 2 不会更新任何更改的内容。

如果我只打开选项卡 2 而不是选项卡 3,那么选项卡 2 在这两种情况下都会更新。

有人对这里可以做什么有任何想法吗?

我已经尝试过各种 invalidate()、notifyDataSetChanged()、invalidateViews() 等方法,但没有任何效果。

我真的不明白为什么在创建一个全新的 ListView (从更改的对象)时旧的 ListView 仍然会显示。

当我选择对所有选项卡使用一项活动时,我是搬起石头砸自己的脚,还是有别的原因?

希望有人可以在这里帮助我。

如果需要一些代码,请告诉是哪一部分,因为该应用程序由大约 15 个类和数千个代码行组成,所以我不会全部发布:)

干杯

have been sitting with this problem for a few days and really can't seem to find the answer myself.

I'm building an application for Android devices. The application will use a socket connection to a PC running a piece of software. The user have some buttons and stuff to change the state of the PC program - this part works well. Basicly by sending text to the PC using the socket.

I also want the Android application to handle incomming events from the PC. These events should alter the UI on the device in order to reflect the state of the PC program.

To understand my problem I will try to explain the structure of the app.
Basicly the app have four activities:

  • A splash activity, which shows a splash screen and after a set time launches the next Activity
  • A connector activity, which have an input where the user will type in the IP of the PC. When the device connects to the PC, the PC will send an xml file to the device basicly containing the state of the program. This xml file is parsed and when parsed without errors this activity launches the next Activity.
  • The next activity is a TabActivity, creating 1 to 5 tabs, depending on the xml that was parsed. Each tab needs an activity which will handle the content for each tab. Here I have chosen to use one activity for all tabs.
  • The last activity is the content activity. This is a ListActivity. When parsing the xml, items will be created and divided into 5 categories - one tab for each of these. When a tab is selected for the first time, the content will be generated, and a ListView object created for that tab. For each item in a category I inflate a layout (xml) and sets the correct graphics based on the state of the object. This works just fine. Switching between the tabs is fine aswell.

So - when switching tabs I check if the ListView for the tab is null, and if that is the case I will create the content, and if not then I will call the "setListAdapter(...)" with the correct "list.getAdapter()", where list will be the list related to the tab.

When receiving an event from the PC i have a Handler taking care of the event. This handler will parse the message, and find out what part of the UI to change, and what Object is related to this part of the UI. Then the state of the Object is changed to matter what - and if the obecjt's tab is currently open the View is changed aswell - if another View is opened a boolean will tell that an object in the tab has changed state.

I have tried to get the View from the ListView variable and change the child elements, but this do not work when the list is not the current list.

Then I tried to create a whole new ListView for the tab when the tab is getting pressed, if this boolean shows that something have been changed (just as it's created in the first place the first time) - this only works for one tab.

I've tried to test it with two of the tabs (2 and 3). The app starts on tab 1. If I then open tab 2 and then 3, sends commands to the PC from both tabs and all is fine. When the PC sends events to the app then only tab 3 will actually update. Working both when the tab is active and inactive (updates when getting active). While tab 2 does not update content on any changes.

If I only open tab 2 and not tab 3, then tab 2 will update in both cases just fine.

Anyone have any ideas to what can be done here?

I have tried all kinds of stuff with invalidate(), notifyDataSetChanged(), invalidateViews() etc, and nothing does the trick.

I really don't understand why, when creating a whole new ListView (from changed objects) that the old ListView will still be shown.

Did I shoot myself in the foot when choosing to use one activity for all tabs, or is this something else?

Hope someone can help me here.

And if some code is needed then please tell which part, as the app consist of ~15 classes and thousands of code lines, so I won't post it all :)

Cheers

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

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

发布评论

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

评论(1

幸福不弃 2025-01-02 15:34:03

我最终为每个选项卡内容提供了一个活动,而不是第一种方法,在第一种方法中,我为所有选项卡内容提供了一个活动,并且在按下选项卡时仅在列表视图的适配器之间进行切换。

将其拆分为每个选项卡的一个活动,可以更改所有内容,无论该活动是否显示。

I ended up with one activity for each tab content instead of the first approach where I had one Activity for them all and just switching between adapters for the listview when a tab was pressed.

Splitting it into an activity for each tab made it possible for all content to be changed no matter if the activity was showing or not.

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