开发者

How to create a spinner while url is looked up?

开发者 https://www.devze.com 2023-03-24 19:03 出处:网络
I would like the user to press a button. When the onClickListener for the button is called, I would like to display a spinner until the webpage is fetched.

I would like the user to press a button.

When the onClickListener for the button is called, I would like to display a spinner until the webpage is fetched.

I don't want to use a AsyncTask because it can only be run once and more than likely the user will click the button more than once after the first url results are retreived.

So how would I go about doing this with this onClickListener?

findIT.setOnClickListener(new V开发者_StackOverflow中文版iew.OnClickListener() {
    @Override
    public void onClick(View v) {
        item = game.getText().toString();
        getUserPreference();
        itemLookup.loadUrl(url);
    }
});


You can still use AsyncTask<>, you just have to create a new one each time.

public void onClick(View v) {
    new DoWhateverTask().execute();
}

Otherwise you can do it yourself using Handlers. Assuming you already have Handlers defined for the UI and a worker thread:

public void onClick(View v) {
    showProgress();
    workerThreadHandler.Post(new Runnable() {
        public void run() {
            doNetworkStuff();  // Which will post progress to the UI thread if needed.
            mainThreadHandler.Post(new Runnable() {
                public void run() {
                    hideProgress();
                }
            });
        }
    });
}

Personally, I think AsyncTask is a nicer solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消