android - Can't return an Array list from AsyncTask into Main Activity? -


I can not understand how the object is returned from the AsyncTask which is ArrayList . Right now if I try to add tours.add (tour); Return to the list inside AsyncTask return code; How can I break from AsyncTask and successfully return these objects to my main activity?

Category with ArrayList:

  Public class Jason Parser {list & lt; Travel & gt; Tourism = new arreelist & lt; Travel & gt; (); Tourism data source datasource; Private string LOGTAG; // reference CTX; // Public JasonPerser (reference reference) {// ctx = context; //} Public listing & lt; Travel & gt; StartJson (Reference Reference) {AsyncHttpClient Client = New AsyncHttpClient (); Client.get ("http://edmondvarga.com/android_dev/party_alarm/test_j.php", New AsyncHttpResponseHandler () {@Success Public Overhead (string response) {try {JSONObject jsonObj = new JSONObject (response); JSONArray Contact = JsonObj.getJSONArray ("user"); for (int i = 0; i & lt; contacts.length (); i ++) {JSONObject json = contacts.getJSONObject (i); Tour tour = new tour ( ); Tour.setTitle (Json.getString ("title"); // string c = json.getString ("content"); Log.i (LOGTAG, "Travel ID created with:" + tour.getTitle ( ); Tours.add (tour);}} Hold (JasonXExation E) {e.printStackTrace ();}}}); Return tourism; }}   

Part of my main activity:

  Public Zero DataData () {JsonParser jp = new JsonParser (); & Lt; Tour & gt; Tourism = jp.startJson (this); // ToursPullParser Parser = New ToursPullParser (); // from the list & lt; Tour & gt; Tourism = parser Prsxml (this); For (Tour tour: Tourism) {Datasource. Crete (tour); }    

Your problem is that the work is asynchronous following the system (the image we understand better Are working on only one core): call startJson to create asyncTask , but now the system is still startJson . Now the system things are fine, this is the right time to start the ASC work and it fills your list. But if you have already returned it then the solution is quite easy. Call another method at the end of onSuccess , which handles results for further operations. (Or just do it on in Success , because it's already on the main thread)

@edit:

  Public Sector Jason Parser {list & lt; Travel & gt; Tourism = new arreelist & lt; Travel & gt; (); Tourism data source datasource; Private string LOGTAG; // reference CTX; // Public JasonPerser (reference reference) {// ctx = context; //} private finals main activity mCallback; Public JsonParser (main activity callback) {mCallback = callback; } Public listing & lt; Travel & gt; StartJson (Reference Reference) {AsyncHttpClient Client = New AsyncHttpClient (); Client.get ("http://edmondvarga.com/android_dev/party_alarm/test_j.php", New AsyncHttpResponseHandler () {@Success Public Overhead (String Response) {Try {JSONObject jsonObj = New JSONObject (Response); JSONArray Contact = JsonObj.getJSONArray ("user"); for (int i = 0; i & lt; contacts.length (); i ++) {JSONObject json = contacts.getJSONObject (i); Tour tour = new tour ( ); tour.setTitle (Json.getString ("title"); // string c = json.getString ("content"); Log.i (LOGTAG, "Travel ID created with:" + tour.getTitle ( ); tours.add (tour);}} Grip (JasonXSpeep e) {e.printStackTrace ();} callback.setResult (Tourism);}}); Public class main activity .... Public Award made data () {JasonPerser JP = new JasonPerson (this); & Lt; Tour & gt; Tourism = jp.startJson (this); } Public Zero Set Result (List & Travel; Travel & Tourism) {Tour (Tour: Tourism) {//.}}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -