(Android practice) AsycTask achieves asynchronous loading

Source: Internet
Author: User

1Implementation Effect description:

1.1
DisplayLoading

 

1.2 after the question information is loaded, bind the question

 

1.3 loading failed, goLoading failed

 

 

2Asynchronous loading implementation

2.1Interface settings

Interface before loading

 

View Code

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "# ffffff"
Android: orientation = "horizontal"
>
<ProgressBar
Android: id = "@ + id/circleProgressBar"
Style = "? Android: attr/progressBarStyleLarge "mce_style = "? Android: attr/progressBarStyleLarge"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
Android: max = "100"
Android: progress = "20"
/>
<TextView android: text = "loading information ..... "
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: textColor = "#000000"
Android: layout_height = "wrap_content"> </TextView>
</LinearLayout>

Loading failure page

 

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "# ffffff"
Android: orientation = "horizontal"
>
<TextView android: text = "network exception, data cannot be loaded" android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </TextView>
<Button android: text = "reload" android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>

</LinearLayout>

2.2Create a custom asynchronous class

 

//AsyncTask    class AsyncLoader_GuessInfo extends AsyncTask<String, Void, Integer>{      @Override      protected Integer doInBackground(String... params) {         int result=0;      try{      gi=GuessDataServiceHelper.GetGuessInfo(params[0]);      result=2;      }         catch(Exception ex){         result=-1;          }              return result;      }     }

 

 

2.3Call asynchronous classes,Different content is displayed based on the feedback.

View Code

/*
* Downloads question information.
**/
Private BegindownHandler mbegindownHandler = new BegindownHandler ();

Class BegindownHandler extends Handler {
@ Override
Public void handleMessage (Message msg ){
AsyncTask <String, Void, Integer> results = new asyncloader_guessinfo(cmd.exe cute (GuessNo );
Try {
If (results. get () = 2)
{
LoadAndBandingData (GuessNo );
}
Else
SetContentView (R. layout. async_error );
}
Catch (Exception ex)
{
SetContentView (R. layout. async_error );

}

}
Public void sleep (long delayMillis ){
This. removeMessages (0 );
SendMessageDelayed (obtainMessage (0), delayMillis );
}
};



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.