I want to wait for asynctask is finished.
MainActivity.java
public class MainActivity extends Activity {
private List<MyObject> lst_object;
private ObjectManager objectManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLayout();
initVariables();
objectManager = new ObjectManager(this);
lst_object = objectManager.getAllData();
**while(!objectManager.getAsyncTaskStatus()) {
}**
System.out.println(lst_object.size() + " items");
}
}
ObjectManager.java
public class ObjectManager {
private Context context;
private Cursor c_object;
private List<MyObject> lst_object;
private MyObject object;
private ProgressDialog progressDialog;
**private boolean status = false;**
public ObjectManager(Context context) {
this.context = context;
lst_object = new ArrayList<MyObject>();
}
**public boolean getAsyncTaskStatus() {
return !status;
}**
class AsyncReadingObjects extends AsyncTask<Void, Integer, String> {
private int totalObject;
@Override
protected void onPreExecute() {
initProgressBar();
progressDialog.setMessage("Reading objects");
System.out.println("onPreExecute finished");
}
@Override
protected String doInBackground(Void... params) {
System.out.println("doInBackground started");
lst_object.clear();
c_object = context.getContentResolver().query(uri_object, null, null,null,"_id ASC");
totalObject = c_object.getCount();
try {
if (c_object.moveToFirst()) {
for (int i = 0; i < totalObject; i++) {
System.out.println(i + ". ITEM");
publishProgress(i);
c_object.moveToNext();
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
c_object.close();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setMessage("Reading objects " + values[0] + "/" + totalObject);
}
@Override
protected void onPostExecute(String values) {
progressDialog.dismiss();
**status = true;**
}
}
public List<Object> getAllData() {
AsyncReadingObjects asyncReadingObjects = new AsyncReadingObjects();
asyncReadingObjects.execute();
return lst_object;
}
Here is the logs :
01-22 00:02:16.220: I/System.out(31507): 0 items
01-22 00:02:16.230: I/System.out(31507): onPreExecute finished
01-22 00:02:16.230: I/System.out(31507): doInBackground started
01-22 00:02:16.250: I/System.out(31507): 0. ITEM
01-22 00:02:16.250: I/System.out(31507): 1. ITEM
01-22 00:02:16.250: I/System.out(31507): 2. ITEM
01-22 00:02:16.250: I/System.out(31507): 3. ITEM
01-22 00:02:16.250: I/System.out(31507): 4. ITEM
01-22 00:02:16.250: I/System.out(31507): 5. ITEM
01-22 00:02:16.250: I/System.out(31507): 6. ITEM
01-22 00:02:16.250: I/System.out(31507): 7. ITEM
01-22 00:02:16.250: I/System.out(31507): 8. ITEM
01-22 00:02:16.250: I/System.out(31507): 9. ITEM
01-22 00:02:16.250: I/System.out(31507): 10. ITEM
01-22 00:02:16.260: I/System.out(31507): 11. ITEM
01-22 00:02:16.260: I/System.out(31507): 12. ITEM
01-22 00:02:16.260: I/System.out(31507): 13. ITEM
01-22 00:02:16.260: I/System.out(31507): 14. ITEM
....
01-22 00:02:16.260: I/System.out(31507): 300. ITEM
I would like to make the following
onPreExecute finished
doInBackground started
0. ITEM
1. ITEM
2. ITEM
3. ITEM
4. ITEM
5. ITEM
6. ITEM
....
300. ITEM
300 items
What I should do ? Thanks for answers.
Put
System.out.println(totalObject)
in onPostExecute().
You may want to add a success field to the AsyncTask, that you can set to false if it fails (in the catch) or true if it makes it through the loop.
Then in your onPostExecute() only print if (success)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.