sr09
sr09

Reputation: 720

How can I update a listview within a window that has been launched by a service?

I am creating a translucent window on top of my application that is visible at all times. It shows some logs as I use the application.

To do this, I created a service that used the window SYSTEM_SERVICE to display a translucent UI on screen. This service is a part of a library. I added the service and the required permissions to my application's manifest file.

I start the service from the library through an observer class. Now, I need to display a list within that translucent window. This list should auto-scroll. The data for the list view is part of the same observer class that keeps adding strings to the list. I created an adapter and listview in my service(as a view) and added it to the translucent window.

I want to update the list now. Accessing the adapter object from this observer class and calling notifyDataSetChanged when the arraylist of strings is updated crashes my app with this:

"android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views."

If I initialize the adapter in the Service, how can I call notifyDataSetChanged from the observer class when there is an update to the list?

I am using app context everywhere. The underlying application should function like it always does.

Any ideas?

Upvotes: 1

Views: 33

Answers (1)

TheIT
TheIT

Reputation: 12219

Just a thought. You could send a message to the service (potentially with the data to update) and allow the service to update the UI. This seems particularly appropriate given that the service was the entity which created the Window.

Upvotes: 1

Related Questions