Reputation: 807
I have an application where I have a separate class named "Images1" which extends AsyncTask, where in I am fetching all the gallery URL via JSON.I am instantiating the AsyncTask class in another activity where I need the arraylist returned by the "Images1" class.However I am unable to get the arraylist. Please suggest what steps should be taken.Thanks in advance
public class Images1 extends AsyncTask<Void, Void, ArrayList<String>> {
ProgressDialog mPreogressDialog;
private static String url = "http://www.tts.com/album_pro/array_to_encode";
JSONParser jParser = new JSONParser();
ArrayList<String> image_urls = new ArrayList<String>();
@Override
protected void onPreExecute() {
mPreogressDialog = ProgressDialog.show(Images1.this, "", "Uploading Document. Please wait...", true);
}
@Override
protected ArrayList<String> doInBackground(Void... params) {
// TODO Auto-generated method stub
//Do your stuff here call ur methods
imagedownload();
return image_urls;
}
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
// TODO Auto-generated method stub
if (mPreogressDialog.isShowing())
mPreogressDialog.dismiss();
}
public void imagedownload(){
JSONObject json = jParser.getJSONFromUrl(url);
try{
JSONObject seo = json.getJSONObject("SEO");
JSONArray folio = seo.getJSONArray("Folio");
JSONArray image_urls1 = new JSONArray();
String s1=seo.getString("Folio");
for(int i=0;i<folio.length();++i)
{
String m = folio.getString(i);
Log.v("M"+i,m);
image_urls.add(m+ ",");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
In the another activity I am instantiating the class and calling it as
Images1 img1;
new img1.execute();
Upvotes: 0
Views: 495
Reputation: 488
Another option is to extend this AsyncTask. Call following piece of code in your activity:
new Images1() {
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
//here you process received result
}
}.execute();
Upvotes: 0
Reputation: 1883
I will suggest you one thing just take new Arraylist and make a for loop and add all the values of the image_urls in that list on the onPostExecute
Method of AsyncTask class and call that arraytlist.
Upvotes: 1
Reputation: 132982
I am unable to get the arraylist.
to get result back in Activity where u are starting AsyncTask u will need to execute AsyncTask as :
ArrayList<String> image_urls=new Images1.execute(params_).get();
but this will block UI Thread execution until AsyncTask doInBackground method execution complete. use Thread
to execute AsyncTask if u are using AsyncTask.get() method to avoid UI freezing
Second way is pass Activity Context to Images1 class using class constructor and do your work inside onPostExecute
. for example :
public class Images1 extends AsyncTask<Void, Void, ArrayList<String>> {
//...your code here..
Context context;
public Images1(Context context){
this.context=context;
}
////....your code here....
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
// TODO Auto-generated method stub
// do your work here...
}
}
use context
for accessing or updating UI from onPostExecute
Upvotes: 2
Reputation: 488
You can use Messages and Handlers here. Take a look at this article: http://mobileorchard.com/android-app-developmentthreading-part-1-handlers/. You can send a message from another thread, containing ArrayList. And then handle this message in handleMessage() function of your activity.
Upvotes: 1