按钮保持,同时在执行AsyncTask的pressed [英] button remains pressed while the Asynctask is being executed

查看:128
本文介绍了按钮保持,同时在执行AsyncTask的pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,它在pressed,执行以下code:

I have a button, which on pressed, executes the following code:

public void onClick(View v) {
            // TODO Auto-generated method stub
            //progressSpin.setVisibility(View.VISIBLE);
            try {
                data=new WebkioskExtractor().execute(username,password).get();
                System.out.println("Data = "+data);                 
            } catch (Exception e) {
                // TODO Auto-geneorated catch block
                e.printStackTrace();
            }
            //progressSpin.setVisibility(View.GONE);
        }

从code清楚,我必须等待AsyncTask的结束,因为我依靠它返回的数据。的问题是,虽然在正在执行的任务(它提取某些数据从Internet)的按钮保持在pressed状态。即使我设置我创建可见进度,它并没有显示出来。

as clear from the code, i have to wait for the AsyncTask to finish because i am relying on the data it returns. the problem is that while the task in being executed (it fetches some data from the internet) the button remains in the pressed state. even if i set the progressbar i created to VISIBLE, it does not show up.

我怎样才能解决这个问题?我想按钮是pressed一次,然后进度应该开始旋转,这是不会发生。

How can i fix this? I would like the button to be pressed once and then the progressbar should start spinning, which isn't happening.

推荐答案

不要使用的get()

data=new WebkioskExtractor().execute(username,password).get();  // Bad! :(

data=new WebkioskExtractor().execute(username,password);  // Good! :) 

这阻止 UI 这就是为什么你的按钮仍然pressed。这也是为什么你的进度没有显示出来(它也运行在 UI )。我假设你开始你的进度在preExecute()驳回() onPostExecute()你的的AsyncTask 。如果不是,这是你应该做的。如果一切在你的的AsyncTask 的设置是否正确,然后删​​除获得()应该解决您的问题。

It blocks the UI which is why your Button remains pressed. This is also why your ProgressBar isn't showing up (it also runs on the UI). I assume you start your ProgressBar in onPreExecute() and dismiss() it in onPostExecute() of your AsyncTask. If not, this is what you should be doing. If everything else in your AsyncTask is set up correctly then removing .get() should fix your problem.

返回从你的结果doInBackground() onPostExecute(),你想要什么,这应该给你。你也可以做任何你需要在 UI onPostExecute()或的<$ C任何其他方法$ C>的AsyncTask 除了 doInBackground()

Return your result from doInBackground() to onPostExecute() and this should give you what you want. You can also do whatever you need to on the UI from onPostExecute() or any other method of the AsyncTask besides doInBackground().

进度

您不需要设置能见度进度。参见下面的例子:

You don't need to set the Visibility on the ProgressBar. See this example:

public class GetUsersTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog progress = ProgressDialog.show(LoginScreen.this, "Downloading Users", "Please wait while users are downloaded");
     // you can create it here

    @Override
    protected void onPreExecute()
    {
        // show it here like so
        progress.setCancelable(false);
        progress.isIndeterminate();
        progress.show();
    }

    @Override
    protected void onPostExecute(Void result) {

            // and dismiss it here
            progress.dismiss();
        }

    } 

    @Override
    protected void onProgressUpdate(Void... values) {
        // can update the ProgressBar here if you need to
    }

    @Override
    protected Void doInBackground(Void... params) {
        ...
         }

使用一个回调

如果你需要从的AsyncTask 其中的不是一个内部类的活动的,然后您可以使用<$ C结果$ C>接口与回调。 <一href="http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648">More这样做,在这个答案

Using a callback

If you need to get a result from AsyncTask which is not an inner-class of your Activity then you can use an interface with a callBack. More about doing that in this answer

AsyncTask的文档

这篇关于按钮保持,同时在执行AsyncTask的pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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