如何让TabActivity中的MapActivity等待从服务器获取Overlay数据?
因此,在我的程序中,我有一个 TabActivity,其中包含两个选项卡,一个选项卡是 ListView,另一个选项卡是通过 Intent 启动的 MapActivity。我希望我的应用程序做的是发送包含手机位置和产品名称的查询,并接收拥有该产品的最近商店的列表,然后在地图选项卡中显示这些商店,并在另一个选项卡中显示它们的列表。当 TabActivity 启动时,应用程序开始侦听位置,并且一旦获取位置,就开始与服务器(在 AsyncTask 中)进行通信。
问题是,当 TabActivity 启动时,它也会启动 MapActivity,因为它位于第一个选项卡中,并且 MapActivity 开始尝试在地图上添加叠加层,但这些叠加层还没有数据,因为 TabActivity 中的 AsyncTask 尚未完成尚未从服务器获取数据,这会导致程序崩溃。
我需要我的应用程序在 TabActivity 首次打开时显示一个空的谷歌地图,并等待从服务器获取数据,然后向地图添加叠加层以标记其上的商店。谁能告诉我如何实现这一目标?我想我应该从 MapActivity 的 onCreate 中删除 Overlay 部分代码,并将其放在其他地方,但我不知道在哪里。
So, in my program i have a TabActivity which contains two tabs, one tab is a ListView and another is a MapActivity that gets started via an Intent. What i want my app to do is send a query with phones location and the name of a product and reveice a list of nearest shops that have this product, then show those shops in the map tab and show the list of them in another tab. The app starts listening for location when TabActivity is started and the communication with the server (in AsyncTask) starts as soon as location is obtained.
The problem is that when TabActivity starts, it also starts the MapActivity because it is in its first tab, and the MapActivity starts to try to add overlays on the map but there is no data yet for these overlays, because the AsyncTask in TabActivity hasnt finished getting data from the server yet, which crashes the program.
I need my app to show an empty google map when TabActivity is first opened and wait for the data to be obtained from server and then add overlays to the map to mark the shops on it. Can anyone show me how can I achieve this? I guess i should remove the Overlay making part of code from onCreate in MapActivity and put it somewhere else but i dont know where.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种可能更好的方法是使用 获取当前选项卡活动ActivityGroup#getCurrentActivity(),检查并转换为预期的 MapActivity,然后调用该 Activity 中的方法:
BroadcastReceiver
解决方案的问题在于广播超出了您的范围应用程序的沙箱,尝试通知设备上正在侦听您意图的任何人,即使只有一个接收者实际上想要与之相关。A potentially better approach would be to get the current tab activity with ActivityGroup#getCurrentActivity(), check and cast to the expected MapActivity, and then call a method in that activity:
The problem with the
BroadcastReceiver
solution is that the broadcast goes beyond your application's sandbox, attempting to notify anyone on the device that is listening for your intent, even though only one receiver actually wants anything to do with it.在您的 AsyncTask 中,重写 onPostExecute 方法并在那里创建 Overlay。 doInBackground 方法完成后将调用此函数。您很可能希望将 doInBackground 内容的结果提供给 onPostExecute。
此处的示例:
In your AsyncTask, override the onPostExecute method and make your Overlay there. This function will get called once the doInBackground method finishes. You'll most likely want to feed the results from your doInBackground stuff to the onPostExecute.
Example from here:
我自己已经解决了这个问题。其实很简单,只需要使用广播接收器即可。
我在 MapActivity 中创建了一个 BroadcastReceiver:
然后在 MapActivities onCreate 方法中注册了它:
最后,我在 TabActivity onPostExecute 方法中使我的 AsyncTask 发送了一条广播,告知已从服务器检索了所需的数据:
I have solved this problem myself. Its really simple, just need to use a broadcast receiver.
I created a BroadcastReceiver in my MapActivity:
Then i registered it in MapActivities onCreate method:
And finally i made my AsyncTask's in TabActivity onPostExecute method send a broadcast telling that the needed data has been retrieved from server: