5

I've 6 different type of List results after completing AsyncTask. And the List results are should be returned to an Activity. For example: List<A>, List<B>, List<C>, List<D>, List<E> and finally List<F>.

This is my AsyncTask:

public class myAsync extends AsyncTask<String, String, List> {

    private List resultList;

    @Override
    protected List doInBackground(String... params) {
        //params[0] is url
        //params[1] is type
        callAPI api = new callAPI(params[0], params[1]);
        // According to type api decides type of List and generates
        return api.getJSON(); // returns a List which already parse JSON
    }

    @Override
    protected void onPostExecute(List result) {
        // Result is here now, may be 6 different List type.
        this.resultList = result 
    }

    // returns result
    public List getResultList() { return this.resultList; }
}

I'll call AsyncTask like this:

myAsync task = new myAsync();
task.execute(params);
List myList = task.getResultList(); // type is uncertain
Log.d("Tag", Integer.toString(myList.size());

You know, I must point out the return type (Result) between <> tags. If I choose specific type for List, it does not work for other types.

Indeed, I already tried to return List<Object> and only List types. But doesn't worked.

I don't want to use 6 different Async. Is it possible to solve this with an only AsyncTask? I guess I need anonymous List or something similiar not sure. Can anyone explain this?

5
  • You can ignore <>, it won't be an error, it just warns you. Commented Jan 26, 2012 at 15:41
  • You mean I should use this definition: AsyncTask<String, String, List> . If it is, what's going to be return types? I already returned all types as a normal List, got an NullException error. Commented Jan 26, 2012 at 15:45
  • onpostexecute should set your list in your other thread, not with a getter method. How would you know if the list was done? you would have to do polling with a getter method. you could do one async and just keep a list of lists of results. Commented Jan 26, 2012 at 15:46
  • Post stacktrace here, I suspect the NullPointer you are getting might be from somewhere else. Commented Jan 26, 2012 at 15:46
  • Marvin's answer explained NullPointer, but not sure how to check the status. Commented Jan 26, 2012 at 16:06

3 Answers 3

9

First, I should point out that the sequence in which you're obtaining the list is not correct. Let me demonstrate:

// Initialize myAsync
myAsync task = new myAsync();

// This will do work for some period of time, this statement DOES NOT 'block'
// till myAsync completes, it will run it in the background
task.execute(params);

// This will return null because by the time you get here, task.execute most
// likely hasn't finished yet.
task.getResultList();

Edit: Now that you've included what you want to do with the list result, here's how you would modify your onPostExecute method:

@Override
protected void onPostExecute(List result) {
  Log.d(TAG, "The returned list contains " +result.size()+ "elements");
  // Do anything else you need to with the returned list here
}

So to summarize, if you needed to do anything else with the returned lists (besides printing their size), for example compare, print items, etc, you need to do that in the onPostExecute method.

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry for syntax error. Of course it has to be task.getResultList(). Corrected now. And you're right, it's early for getResultList() method. How can i call?
@OgulcanOrhan Just as I pointed out in your previous question, you usually want to use the onPostExecute method of your AsyncTask to do whatever it is you need to do with the list(s). Edit your question to include what you do with task.getResultList() and I can give you a better answer.
Updated doInBackground and onPostExecute. I hope it's clear now.
@OgulcanOrhan Please also add all the code related to task.getResultList();. I.e. What do you do with the list that's returned? (That's what I was asking for). For example, do you print it out, do you update a listview,... ?
@OgulcanOrhan Without knowing any of the details of how your intents work (fair warning), I would say yes, you need to launch your intents from within onPostExecute. In any case, try all this out and if you run into issues, create a new question with all your new details and we'll try to help you out. Hope this helped.
|
2

Depending on what the contents of the list is (What are A, B, C etc.), you might be able to make an interface for them.

So if A, B, C etc are objects, you can make an Interface that we call ListContentInterface. Each of your elements must then implement the ListContentInterface. You can then point out the type as:

List<ListContentInterface>.

After that you can then test what the contents of the list really is, by taking the first element of the list, and checking it's class:

if(element.getClass() == ClassToTestFor.class) ...

If the objects have any methods in common, you should specify them in the interface. If they have ALL methods in common, you can use the List list directly, without testing for the class of the objects (As they can all do what the interface defines).

I hope this makes some sense to you. It might not be the best use of interfaces or the best solution, but it might solve your problem.

2 Comments

Yes I may able to make an interface for these 6 different object, and I will. But what will change? I couldn't understand next steps.
When you have all the items in an interface, you can create a list containing objects that implement that interface. That way you can use the same type of list, for all your objects. After that, if you need to do something specific to the different item types, you can just take the elements out of the list, and test which class they are, before doing the item-specific logic to them.
0

I'm solving this kind of problem using execute().get();

Example

call from activity

    ArrayList<String> arraylistitem= new jsonparsing(getActivity()).execute().get();

    In async task 

    public class jsonparsing extends AsyncTask<Void, Void, ArrayList<String>>
    {
    public jsonparsing(Activity activity)
        {
            this.activity = activity;
            arraylistitem = new ArrayList<String>();        
        }

        protected ArrayList<String> doInBackground(Void... arg0) 
        {
               // Do anything else you need to with the returned list here
             return arraylistitem;
            }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.