java - Instance variable of Activity not being set in onPostExecute of AsyncTask or how to return data from AsyncTask to main UI thread -
I am trying to find the right way to create an asyncTask to get some data from the internet and then take it To pass the data and bundle it in an intent and for a new activity (a list display). Therefore, in the first activity, I have only one edit text and button. In the event of an OnClick, the job should be called and when it is complete, the data should be bundled inside the intent and the next activity should be passed. The problem occurs when I take the results on poststation and set them in an example variable of main activity, this task is complete when the task is completed. Here's the barebones version of code:
The public class uses SearchActivity Activity OnClickListener {Fixed last stringTAG = "SearchActivity"; Private Editing Test Search Input; Private Button Search BTN; Private site list places; /** Called when the activity is first created. * / @ Override Crate on Public Zero (Bundle Saved Instantstate) {Super. Contents (Saved Instantstate); SetContentView (R.layout.search_activity); SearchBtn = (button) findViewById (R.id.button_search); SearchInput = (edit text) findViewById (R.id.search_input); SearchBtn.setOnClickListener (this); } Public void onClick (see v) {if (v == searchBtn) {string input = searchInput.getText (). ToString (); If (input! = Null & amp; input length!) = 0) {try {new TestTask () Executed; Bundle bundle = new bundle (); Bundle Vipul ("places", places); I = new intent (this, SearchResultsActivity.class); I.putExtras (bundle); StartActivity (i); } Hold (Exception pre) {ex.printStackTrace (); }}}} Private Class Test Task Async Task and Late; String, wide, placelists & gt; {Personal progress DIOLOG DLG = new progression DLOG (Search Activity) this; @PreExecute override protected zero () {dlg.setMessage ("test"); Dlg.show (); } @ Override Protected Playlist Do Inbackground (String ... Args) {Try {Return to New Place-Lock}. Retrieve Results (Args [0]); } Grip (Exception E) {// TODO Auto generated blocking block e.printStackTrace (); } Return new locations list (); } Secure zero at the @OverridePostExecute (Location list) {SearchActivity.this.places = Results; If (dlg.is is showing ()) dlg.dismiss (); }}}
When I debug the application on which I see PostExecute is filled with a valid listlist result, then after the execution of the function, to clear the frequency variable locations Why is set up for? I'm going wrong about "data back" from an asyncTask?
According to an asyncTask definition, executed in a different thread such that, after you execute the result Can not expect to be available instantly. Instead, you should trigger the new stimulus of the completion of AsyncTask:
Click Public Zero (see V) {if (v == searchBtn) {string input = searchInput.getText () .Wire(); If (input! = Null & amp; input length!) = 0) {try {new TestTask () Executed; } Hold (Exception pre) {ex.printStackTrace (); }}}} Private Zero Startup (placelist location) {bundle bundle = new bundle (); Bundle Vipul ("places", places); I = new intent (this, SearchResultsActivity.class); I.putExtras (bundle); StartActivity (i); } Private Class Test Task Async Task & lt; String, wide, placelists & gt; {... Protected void on @OverridePostExecute (location list) {startSearch (results); If (dlg.is is showing ()) dlg.dismiss (); }}
Comments
Post a Comment