开发者

Android onClick() tasks all happen at once

I'm trying to develop a feature for my app that pulls a string of text off of the internet (sort of like twitter, but a lot more basic) and displays it on the screen in a permeanent window. The user sees a large box with a refresh button next to it, and a small space below both where I would like to have a little progress monitor (just a TextView) which displays "Refreshing..." as soon as the开发者_运维技巧 refresh button is clicked, and then "Refresh successful!" once the string of text has been successfully pulled from the internet and displayed. This is just to reassure the user that something is actually happening when they press the button.

After a lot of research I've come to the conclusion that the way to update the TextView is to use a handler to execute a runnable which will update the text in the TextView. So my code looks like this:

    refreshbutton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View abc)
        {
                //display "Refreshing..."
                refreshhandler.post(refreshingmsg);

            /*Code to pull the string of text from the internet goes here (not shown)*/

                //display "Refresh successul!"
                successfulhandler.post(successfulmsg);

                //clear the "Refresh successful" message after 2 seconds
                clearhandler.postDelayed(clearmsg, 2000);
            }
    });

I hope that sort of makes sense. My issue is that these things all happen at once: I want the "Refreshing..." message to display, THEN for the phone to connect to the internet to find the string of text for the update, and THEN for the "Refresh Successful" message to display once the internet string has been successfully displayed. But what actually happens is that none of the commands in the onClick method actually happen until the phone has already pulled the message from the internet so the "Refreshing..." message isn't displayed at all. So what actually happens when the user clicks the button is that nothing happens for a second or two (presumably because the phone is busy pulling the string from the internet), then the string from the internet is suddenly displayed along with the message saying "Refresh Successful!".

So why is this happening? Is onClick supposed to work this way? If so, is there a workaround? I haven't posted my runnable and handler statements because I'm not sure it'll make any difference... but if it would help then I can post them!

Also, you can probably tell from this that I haven't been doing android (or indeed java) for very long, but I'm trying my best so please be gentle! Thanks :)


What is happening is that your code is locking on the network communication and doesn't let your app update itself with the handler value.

You should create an AsyncTask to that work for you.

On the OnPreExecute method, change your UI to show the user that you are refreshing your content. Reading your code it appears that you are updating an image, do that here.

On the doInBackground method do your actual network communication, an optional step is to use onProgressUpdate(Progress...) to update your UI to notify the user that the operation is progressing.

On the onPostExecute method you them update your UI to reflect the new content.

For more info check the documentation in AsyncTask, you can also search StackOverflow, there's a lot of good questions about it here.

As a side note, I don't recommend that you use handlers at all, AsyncTasks are easier to use and the code looks better.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜