I have work that needs to be done in this order:
In main activity: -Show progress dialog -Fetch and Parse XML from web -dismiss dialog -Switch to ListView activity that uses array from parsed XML in first activity
My problem is that because I have my fetching/parsing work in an ASyncTask, the main activity immediately tries to load the new activity even the I need the fetch/parse work to be done first because it gets the data for the listView (the new activity). How can I make sure the new Activity does not load until the ASyncTask work is complete?
Here is an example of what I am working with:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText text = (EditText) findViewById(R.id.entry);
input = text.getText().toString();
input = input.replace(" ", "%20"); //replace spaces with %20 for URL specification
WeatherRetrievalTask wt = new WeatherRetrievalTask();
wt.execute();
Bundle bun = new Bundle();
bun.putParcelableArray("in", (Parcelable[])weather);
Intent myIntent = new Intent(view.getContext(), WeeklyDisplayActivity.class);
myIntent.putExtras(bun);
startActivityForResult(myIntent, 0);
}
});
}
*Ignore this bundle work, as this is a topic for another question I need to ask...
So the startActivityForResult needs to wait until the ASyncTask is completed and the dialog is dismissed before it runs b开发者_如何学JAVAecause it needs the weather data first in order to populate the list view.
You should do that 'Switch to ListView activity' in that onPostExecute() in you ASyncTask.
Do not write the code, that will use the result of ASyncTask just after calling its execute(), because it will execute before completing the asynctask which will be worked as another thread.
So do the work that need use the result of asynctask doinbackground() in its onPostExecute() method
精彩评论