Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return anonymous List or ArrayList on AsyncTask in Android
    primarykey
    data
    text
    <p>I've 6 different type of <code>List</code> results after completing <code>AsyncTask</code>. And the <code>List</code> results are should be returned to an <code>Activity</code>. For example: <code>List&lt;A&gt;</code>, <code>List&lt;B&gt;</code>, <code>List&lt;C&gt;</code>, <code>List&lt;D&gt;</code>, <code>List&lt;E&gt;</code> and finally <code>List&lt;F&gt;</code>. </p> <p>This is my <code>AsyncTask</code>:</p> <pre class="lang-java prettyprint-override"><code>public class myAsync extends AsyncTask&lt;String, String, List&gt; { 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; } } </code></pre> <p>I'll call AsyncTask like this:</p> <pre class="lang-java prettyprint-override"><code>myAsync task = new myAsync(); task.execute(params); List myList = task.getResultList(); // type is uncertain Log.d("Tag", Integer.toString(myList.size()); </code></pre> <p>You know, I must point out the return type (Result) between &lt;> tags. If I choose specific type for <code>List</code>, it does not work for other types. </p> <p>Indeed, I already tried to return <code>List&lt;Object&gt;</code> and only <code>List</code> types. But doesn't worked. </p> <p>I don't want to use <strong>6 different <code>Async</code></strong>. Is it possible to solve this with an only <code>AsyncTask</code>? I guess I need anonymous List or something similiar not sure. Can anyone explain this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload