Android uses AsyncTask to listen for asynchronous loading actions
There are many examples of how to use AsyncTask on the Internet, which is also very convenient to use. I won’t talk about the specific usage method here. Students can Google it, there are many.
Scene simulation
When we load a list, such as GridView, we consider the practice of not blocking the UI, and we generally use thread Thread, Timer or AsyncTask. These operations are opening another thread in the background to find data for us. The specific data obtained needs to be updated with Handler. AsyncTask also uses the same Handler as it encapsulates the Handler in the onPostExecute execution operation. This operation may cause a problem. For example, if you have a list to update the database, you use AsyncTask asynchronous operation to update the UI, and your needs areWhen I enter this list, I count the number of data in this list or make the status of a certain row of data to be selected.. The traditional approach is to directly new an AsyncTask class and let it execute(); and then operate the UI. The idea is correct, but there is a problem we need to notice, because it is the way to load data asynchronously, and your data volume is relatively large, or it may take a certain amount of time to find data. At this time, use AsyncTask to perform asynchronous loading and update the UI and then operate the UI object, which may report a null pointer.
The problem arises that we all know that the execution of the code is from top to bottom. When you use asynchronous loading data, the code lets you execute asynchronous operations without paying attention (multi-threading), and continues to execute the code. The code below is the UI in the operation list. At this time, it can be imagined that the asynchronous loading data has not yet ended and your UI has not been updated. These lists should be empty, and operating an empty list will report a null pointer.
Analyze the problem
Students who have used AsyncTask know that a minimum of asynchronous loading of data must be rewrite the following two methods:
doInBackground In the background, all time-consuming operations can be placed here. Note that this cannot be operated directly hereUI。
onPostExecute is equivalent to the way Handler handles the UI. Here you can use the result processing operation UI obtained in doInBackground.
If necessary, you have to rewrite the following three methods, but it is not necessary:
onProgressUpdate You can use the progress bar to increase user experience.
onPreExecute �
onCancelled
According to the above ideas, it can be seen that the final data loading and displaying this series of operations are all in the onPostExecute method. So how to listen to all UIs have been processed in onPostExecute , and then we will execute our own operations?
Solve the problem
Here are my own ideas for solving this problem. Friends who have better ideas are welcome to discuss with the post.
First create an interface
private interface isLoadDataListener { public void loadComplete(); } Declare this interface variable
private isLoadDataListener loadLisneter;
Assign values to the interface to obtain the interface object
public void setLoadDataComplete(isLoadDataListener dataComplete) { = dataComplete; }
After that, the interface is called after the onPostExecute processing UI of AsyncTask is completed. Here is a class AsyncTask used by my previous project:
class loadGridAsyncTask extends AsyncTask<Integer, Integer, AppsAdapter> { private int poindex; public loadGridAsyncTask(int positionindex) { = positionindex; } @Override protected AppsAdapter doInBackground(Integer... params) { // TODO Auto-generated method stub // (); Cursor temp = (poindex); loadPage(mApps, temp); (); return new AppsAdapter(, mAppsModel); } @Override protected void onPostExecute(AppsAdapter result) { gridViewExt itemGrid = (gridViewExt) viewFlipper .getChildAt(poindex); (pageColumnCount); (result); if (loadLisneter != null) { (); } } }
Through the above code, we get an interface returned after data loading is completed. The next problem is that we use this interface to process our UI, such as letting a certain UI select and get the number of UIs in this list, etc., see the following code:
new loadGridAsyncTask(1).execute(); setLoadDataComplete(new isLoadDataListener() { @Override public void loadComplete() { // TODO Auto-generated method stub //Execute the operation you want here. When the UI update is completed, the code here will be automatically called } });
Thank you for reading, I hope it can help you. Thank you for your support for this site!