android - Can't return an Array list from AsyncTask into Main Activity? -
I can not understand how the object is returned from the Category with ArrayList: Part of my main activity: Your problem is that the work is asynchronous following the system (the image we understand better Are working on only one core): @edit: 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?
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; }}
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); }
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)
Comments
Post a Comment