0

I have the queryAppIcon() method that queries and stores images in the array appIconDrawable. However, this task is taking forever (i.e. click button to view images, but I get a 5 second pause on my app before the new screen appears) so I decided to use an AsyncTask. However, I'm calling a method with no parameters, so I can't pass in any information (i.e. first ParseResult.get(0).get("Icon"), then ParseResult.get(1).get("appIcon"), ..). Also, no information is being passed back here as my appIconDrawable[i] doesn't equal anything since I don't know how the processFinish method would pass back information to the array. Am I setting it up right? What should I be doing? Thanks

// global vars
final Drawable[] appIconDrawable = null;
int i;

public Drawable[] queryAppIcon() throws ParseException, IOException {
    ParseQuery<ParseObject> query = ParseQuery.getQuery("AndroidStoreContent");
    query.whereExists("appIcon");
    List<ParseObject> ParseResult = query.find();
    // initialize Drawable array
    final Drawable[] appIconDrawable = new Drawable[ParseResult.size()];

    for (i = 0; i < ParseResult.size(); i++) {
        ParseFile pf = (ParseFile) ParseResult.get(i).get("appIcon");
        startDownload(pf);
    }
    return appIconDrawable;
}

public void startDownload(ParseFile pf) {
    new DownloadImageTask(this).execute(pf);
}

public class DownloadImageTask extends AsyncTask<ParseFile, Void, Drawable> {

    private AsyncResponse ar;

    DownloadImageTask(AsyncResponse ar) {
        this.ar = ar;
    }

    @Override
    protected Drawable doInBackground(ParseFile... pf) {
        return fetchDrawable(pf[0]);
    }

    protected void onPostExecute(Drawable result) {
        ar.processFinish(result);
    }

    public Drawable fetchDrawable(ParseFile pf) {
        InputStream is;
        try {
            is = (InputStream) new URL(pf.getUrl()).getContent();
            return Drawable.createFromStream(is,null);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@Override
public void processFinish(Drawable d) {
    appIconDrawable[i] = d; // i also tried testing appIconDrawable[1] = d and the app loaded with all blank images and then crashes
}

This is the interface, AsyncResponse:

public interface AsyncResponse {
    void processFinish(Drawable d);
}
Ahmad
  • 69,608
  • 17
  • 111
  • 137

1 Answers1

0

Whatever you pass to execute() is the parameter in doInBackground(), what you return from doInBackground will be passed to onPostExecute(). And you can also use pubishProgress() and onProgressUpdate() to show what's happening during the progress...

Is that what you were looking for?

jpm
  • 3,300
  • 1
  • 19
  • 29
  • Yes. If I call the async task with DownloadImageTask(this).execute(ParseFile) but I just pass in one ParseFile to doInBackground(ParseFile...pf), when I call fetchDrawable do I call fetchDrawable[0] even if I passed in a single ParseFile and not an array? – Ermias Asghedom Dec 02 '13 at 16:42
  • ah. yes exactly. if you only pass one param, this is basically an array with one element e.g. pf[0] – jpm Dec 02 '13 at 16:44
  • I don't know how exactly that kind of "param list" with the "..." works, but yeah, you can access one element with pf[0]. And pass multiple with "execute(pf1, pf2, pf3)"... – jpm Dec 02 '13 at 16:48
  • 2
    @ErmiasAsghedom search for var args – Raghunandan Dec 02 '13 at 16:53
  • 1
    thx, here's an explanation: http://stackoverflow.com/questions/3158730/java-3-dots-in-parameters – jpm Dec 02 '13 at 17:01
  • Sorry one last question. So when I get to processFinish(Drawable d) {...}, I have the final drawable object I want, but how do I pass this back to my queryAppIcon's `appIconDrawable[i] = ...`? Because I call the start method before that and it doesn't return anything – Ermias Asghedom Dec 02 '13 at 17:16
  • you need to handle that in processFinish(). That's what Async means, it doesn't wait for the task to be finished... The way I see it you pass 'this' as a "AsyncRespons" so I assume you also have access to the appIconDrawable array in processFinish()...? Just assign it in there – jpm Dec 02 '13 at 17:23
  • Yeah that's what I did. I edited my original post with my updated code. Could you please take a look? – Ermias Asghedom Dec 02 '13 at 17:38
  • Sorry, forgot to mention that the app crashes when this is executed – Ermias Asghedom Dec 02 '13 at 17:51
  • 'i' is not the value that belongs to the PF at this moment (when you start the asynctask, the for loop continues, i is increased, and some time later the processFinish is called...). If you don't need the drawables to be at the same position as they are in the ParseResult, you might want to use an ArrayList and simply add the drawables to its end in processFinish(). If you need the position, then you'll need to find a way to pass the position (i) on to the AsyncTask and then to processFinish... – jpm Dec 03 '13 at 09:54