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
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.