Android uses Asynctask to listen for asynchronous loaded actions
The use of Asynctask has many examples on the internet and is very convenient to use. Here is not detailed the use of specific methods, students can Google a bit, a lot.
Scene simulation
When we are loading a list, such as the GridView, this time we take into account the practice of not blocking the UI, usually using thread threads, Timer, or using Asynctask, and these operations are in the background to open a separate thread for us to find data, The specific data needed to be updated using handler to update Ui,asynctask is also the same as the handler that is used only to encapsulate handler in the OnPostExecute execution operation. And this operation may create a problem, such as you have a list update database is using the Asynctask asynchronous operation to update the UI, and your requirement is when I come in the list of statistics on the number of data in this list or to make a row of data status is selected . The traditional approach is to direct the new one Asynctask class Let it execute (); The UI is then manipulated. The idea is right, but there is one problem we should note because it is the way to load data asynchronously, and you have a lot of data and maybe it takes a while to find the data, then you use Asynctask to perform an asynchronous load, update the UI and then manipulate the UI object, and you may report a null pointer.
The problem is, we all know that the execution of the code is Top-down execution, when you use asynchronous loading data, the code allows you to perform asynchronous operations on the No matter (multithreading), and continue to execute the code, you are the following code is the operating list of the UI, then imagine, The asynchronous load data is not finished yet the UI has not been updated, and your list should be empty, and an empty list of operations will report null pointers.
Analyze problems
The students who have used Asynctask know that an asynchronous load of data must at least override the following two methods:
Doinbackground background execution, more time-consuming operations can be placed here. Note that the UI cannot be directly manipulated here.
OnPostExecute corresponds to the way handler handles the UI, where you can use the results obtained in Doinbackground to manipulate the UI.
If necessary, you will have to rewrite these three methods, but not necessarily:
Onprogressupdate can use the progress bar to increase the user experience degree.
OnPreExecute here is the interface when the end user calls Excute
Oncancelled the action to be done when the user calls Cancel
According to the above ideas, you can see that the final data loading and display this series of operations are in the OnPostExecute this method, then how to monitor all the UI has been done in the onpostexecute inside processing, and then to perform our own operation?
Solve the problem
Here gives me to solve this problem of ideas, have a better idea of friends welcome to discuss together.
First create an interface
Private interface Isloaddatalistener {public
void LoadComplete ();
}
Declare this interface variable
Private Isloaddatalistener Loadlisneter;
Assigning values to interfaces to get interface objects
public void Setloaddatacomplete (Isloaddatalistener datacomplete) {
this.loadlisneter = datacomplete;
}
The interface is then invoked after the Asynctask OnPostExecute processing UI is completed, and a Asynctask class used by my previous project is given below:
class Loadgridasynctask extends Asynctask<integer, Integer, appsadapter> {
private int poindex;
Public loadgridasynctask (int positionindex) {this.poindex = Positionindex;
@Override protected Appsadapter doinbackground (Integer ... params) {//TODO auto-generated method stub
Mappsmodel.clear ();
Cursor temp = Dbhelper.querypagebyid (Poindex);
LoadPage (Mapps, temp);
Temp.close ();
return new Appsadapter (Stb.this, Mappsmodel); @Override protected void OnPostExecute (Appsadapter result) {Gridviewext Itemgrid = (gridviewext) VIEWFL
Ipper. Getchildat (Poindex);
Itemgrid.setcolumncount (Pagecolumncount);
Itemgrid.setadapter (result);
if (loadlisneter!= null) {loadlisneter.loadcomplete (); }}
With the above code, we get an interface that is returned after the data is loaded, and the next problem is that we use this interface to handle our UI, such as having a UI selected, getting the number of UI for the list, and so on, and looking at the following code:
New Loadgridasynctask (1). Execute ();
Setloaddatacomplete (New Isloaddatalistener () {
@Override public
void LoadComplete () {
//TODO auto-generated method Stub
//Here executes the operation you want, and automatically calls this code when the UI update is complete }
.
Thank you for reading, I hope to help you, thank you for your support for this site!