如何添加进度条到独立的AsyncTask? [英] How to add progress bar to standalone Asynctask?

查看:125
本文介绍了如何添加进度条到独立的AsyncTask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AsyncTask的是在自己的活动。我将它传递一个字符串值,并将其连接到我的网络服务和下载的基础上我通过在名称,返回的结果集的Json JSON数据。伟大的作品。

I have an asynctask that is in its own activity. I pass it a string value and it connects to my web service and downloads Json data based on the name I pass in, returning the Json resultset. Works great.

我想一个进度微调添加到AsyncTask的,但我阻碍至于如何做到这一点。我细读了和其他许多博客和接近,但还没有找到解决办法。看来我要么需要具备的AsyncTask在一个Activity类来获取上下文或我在上下文作为参数传递 - 但我需要输入参数是字符串。我读过有关建设一个可容纳字符串和上下文参数对象的可能性,但我很新的Java和不知道如何建立类似的东西也没有,我发现一个很好的解释如何这样做。所以常常一个解释得到一直到我需要什么,然后说,......然后,你做X,就是这样,当X是什么,我需要知道的。

I'd like to add a progress spinner to the asynctask, but I'm stymied as to how to do it. I've perused this and many other blogs, and come close but have not yet found the solution. It seems I either need to have the asynctask in with an Activity class to get the context or I have to pass in the context as a parameter -- but I need the input parameter to be String. I've read about the possibility of building an Object that could hold the String and a Context parameter, but I'm very new to Java and don't know how to build something like that nor have I found a good explanation of how to do so. So often an explanation gets right up to what I need and then says, "... and then you do X and that's it," when X is what I need to know.

我要的只是一个微调的thingie抡而发生的下载。没有文字,没有对话,只是一个微调。

All I want is just a spinner thingie to whirl while the download happens. No text, no dialog, just a spinner.

推荐答案

添加一个进度条(这是它的实际调用,纱厂都像在Android的菜单下拉)到活动的布局你在哪里初始化您的AsyncTask

Add a ProgressBar(this is what it's actually called, Spinners are like drop down menus in Android) to the layout of the Activity where you're initializing your AsyncTask.

然后,让两个功能 startProgress() stopProgress(),它启动和停止进度条。

Then make two functions startProgress() and stopProgress(), which start and stop the progress bar.

给你的AsyncTask的活动的引用,或者通过初始化或执行过程中发送,或者在你的AsyncTask制作功能 setActivity(MyActivity活动)并调用它的您AsyncTask的初始化和执行。

Give your AsyncTask a reference to the Activity, either by sending it during initialization or execution, or making a function in your asyncTask setActivity(MyActivity activity) and call it between your AsyncTask initialization and execution.

重写上preExecute()您的AsyncTask来调用 activity.startProgress() onPostExecute()调用 activity.stopProgress()

Override the onPreExecute() of your AsyncTask to call activity.startProgress() and onPostExecute() to call activity.stopProgress().

编辑:您也可以尝试通过在你的AsyncTask的构造函数的进度的参考。得到你的活动的onCreate()方法参照进度,然后在AsyncTask的构造函数中添加它。在上preExecute() onPostExecute()的AsyncTask的方法,启动和停止进度条因此。

You can also try passing a reference to the ProgressBar in the constructor of your AsyncTask. Get the reference to the ProgressBar in the onCreate() method of your activity, then add it in the AsyncTask constructor. In the onPreExecute() and onPostExecute() methods of the AsyncTask, start and stop the progress bars accordingly.

这篇关于如何添加进度条到独立的AsyncTask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆