57276

Using AsyncTasks as separate public classes

Question:

this question is a continuation of what i have asked orginally <a href="https://stackoverflow.com/questions/9690055/type-mismatch-cannot-convert-from-asynctask" rel="nofollow">here</a>

i am trying to find a better way to pass the ArrayList from AsyncTask to the Activity (any activity that calls the AsyncTask) so in my situation i have a SongsManager.java which extends to AsyncTask as you can see below.

my question is, It does not returning me the songsList i am getting size of 0... any idea what i am missing or do i need to tweak or add any code further?

SongsManager.java

public class SongsManager extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> { public interface SongsMasterCallback { void showSongList(List<HashMap<String, String>> result); } private SongsMasterCallback mCallback; public SongsManager (SongsMasterCallback callback) { mCallback = callback; } // Constructor //public SongsManager(){ } ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); @Override protected ArrayList<HashMap<String, String>> doInBackground(Void... params) { //populating all the data.... HashMap<String, String> map = new HashMap<String, String>(); //......... songsList.add(map); } return songsList; } @Override protected void onPostExecute(ArrayList<HashMap<String, String>> result) { // TODO Auto-generated method stub //super.onPostExecute(result); mCallback.showSongList(result); } }

main.java

public class Main extends Activity implements SongsMasterCallback { public void showSongList(List<HashMap<String, String>> result) { this.songsList = (ArrayList<HashMap<String, String>>) result; // then do something with the list here } @Override public void onCreate(Bundle savedInstanceState) { new SongsManager(this).execute(); Log.d("songsSize", "string : "+songsList.size()); //it shows 0 ??? } }

Answer1:

And if you pass the reference of the songsList to the AsynTask?

public class SongsManager extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> { private ArrayList<HashMap<String, String>> songsList; public SongsManager (ArrayList<HashMap<String, String>> songsList) { this.songsList = songsList; } @Override protected ArrayList<HashMap<String, String>> doInBackground(Void... params) { //populating all the data.... HashMap<String, String> map = new HashMap<String, String>(); //......... songsList.add(map); } return songsList; } @Override protected void onPostExecute(ArrayList<HashMap<String, String>> result) { // TODO Auto-generated method stub //super.onPostExecute(result); } }

and in the main.java call new SongsManager(songsList).execute();

Answer2:

Try this

public class SongsManager extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> { public interface SongsMasterCallback { void showSongList(List<HashMap<String, String>> result); } private SongsMasterCallback mCallback; public SongsManager (SongsMasterCallback callback) { mCallback = callback; } // Constructor //public SongsManager(){ } @Override protected ArrayList<HashMap<String, String>> doInBackground(Void... params) { ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); //populating all the data.... HashMap<String, String> map = new HashMap<String, String>(); //......... return map; } @Override protected void onPostExecute(ArrayList<HashMap<String, String>> result) { // TODO Auto-generated method stub //super.onPostExecute(result); songsList.add(result); mCallback.showSongList(result); }

This is happening because you are creating object of songsList in UI thread and adding data in background thread.

Recommend

  • Value cannot be null.Parameter name: stream error while reading the contents of embeddded file
  • psycopg2 and SQL injection security
  • Scala Slick Database Views
  • ExtJS 4 Spring 3 file upload. Server sends bad response content type
  • How to clear out the contents of a map when clear() method call throws UnsupportedOperationException
  • Update Search Results to Lazy Adapter in android
  • How to apply async task into this
  • Java : How to tint this PNG programmatically?
  • Spring Data JPA custom method causing PropertyReferenceException
  • Can I display google adwords (AdView) in javafx on android
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Acquiring multiple attributes from .xml file in c#
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Why joiner is not used after Sequence generator or Update statergy
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Android Google Maps API OnLocationChanged only called once
  • File not found error Google Drive API
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?