Asynctask API调用问题
我目前正在使用 AsyncTask 进行 API 调用并用数据填充列表。 我有一个子菜单,其项目可以调用 AsyncTask 来填充数据,问题是,如果我快速单击,我最终会得到合并的结果,显然是因为 AsyncTask 彼此同时运行。
处理这种情况的最佳方法是什么?抱歉,如果这是一个业余问题。
I'm currently using an AsyncTask to make an API call and populate a list with data.
I have a Sub Menu whose items can call the AsyncTask to populate the data, problem is that if i click quickly i end up with merged results obviously cause the AsyncTask is running at the same time as each other.
What is the best way to handle a situation like this? Sorry if this is a amateur question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将使用
ProgressDialog
来显示内容正在更新,更新完成后,dismiss()
对话框。发生这种情况时,您应该确保ListView
上不接受触摸输入。 (当ProgressDialog
在前面时,默认情况下可能会发生这种情况,我目前不记得了......)看看这个 链接 为例。
I would use a
ProgressDialog
to show that content is being updated, and when the update is complete,dismiss()
the dialog. While this is happening, you should make sure that you are not accepting touch input on yourListView
. (this may happen by default when theProgressDialog
is in front, I am not remembering currently...)Take a look at this link for an example.
您只能配置一次运行一个异步任务。
一个布尔变量,异步任务启动后立即设置为 true,完成后立即设置为 false。
下一次调用可以等待它被设置为 false。您还可以限制最短时间,之后才进行 API 调用才有意义。
You could configure only a single Asynch task to run at a time.
A boolean variable which is set to true as soon as the asynch task starts and set to false as soon as it finishes.
Next call could wait for this to be set to false. You could also rate limit what is the min time after which only making the API call makes sense.