Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it Possible to get results of AsyncTask as an Arraylist Hashmap
    primarykey
    data
    text
    <p>I have at least three activities in my app right now that use an AsyncTask to return JSON results into an ListView. I've started work on the app, but another person will take over development as soon as he gets the basics down, so I want to try and make things as easy to use as possible. This means that I'm trying to turn as much repeatable code into callable functions as possible, so instead of needing to copy/paste/reuse 30-40 lines of code each time they need query a webservice, they can just pass in parameters to a function.</p> <p>Currently, I have the following in an activity that pulls a list of gym classes from a mysql database via a php webservice:</p> <pre><code> class LoadAllClasses extends AsyncTask&lt;String, String, String&gt; { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); // pDialog = new ProgressDialog(Checkin.this); // pDialog.setMessage("Loading products. Please wait..."); // pDialog.setIndeterminate(false); // pDialog.setCancelable(false); // pDialog.show(); } /** * getting All products from url * */ @Override protected String doInBackground(String... args) { // Building Parameters List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); params.add(new BasicNameValuePair("tag", getclasses_tag)); // getting JSON string from URL JSONObject json = jParser.makeHttpRequest(SmashGyms.WEBSERVICE_URL, "POST", params); // Check your log cat for JSON response Log.d("CheckinDialog", json.toString()); try { // Checking for SUCCESS TAG int success = json.getInt(TAG_SUCCESS); if (success == 1) { // classes found // Getting Array of Classes classes2 = json.getJSONArray(TAG_CLASSES); // looping through All Classes for (int i = 0; i &lt; classes2.length(); i++) { JSONObject c = classes2.getJSONObject(i); // Storing each json item in variable String id = c.getString(TAG_CLASSID); String name = c.getString(TAG_CLASSNAME); //String day = c.getString(TAG_DAY); // creating new HashMap HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); // adding each child node to HashMap key =&gt; value map.put(TAG_CLASSID, id); map.put(TAG_CLASSNAME, name); //map.put(TAG_DAY, day); // adding HashList to ArrayList allclasseslist.add(map); Log.d("map: ", map.toString()); } } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ @Override protected void onPostExecute(String file_url) { // dismiss the dialog after getting all products runOnUiThread(new Runnable() { @Override public void run() { /** * Updating parsed JSON data into ListView * */ adapter = new SimpleAdapter(CheckinDialog.this, allclasseslist, R.layout.checkin_item, new String[] { TAG_CLASSID, TAG_CLASSNAME }, new int[] { R.id.pid, R.id.name }); setListAdapter(adapter); } }); //pDialog.dismiss(); // updating UI from Background Thread } } </code></pre> <p>I'd like to move this to another class that I have, called "WebServiceTasks", so that I can call something like this in the activity's OnCreate():</p> <pre><code>allclasseslist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); allclasseslist = new WebServiceTasks.LoadAllClasses().get(); adapter = new SimpleAdapter(CheckinDialog.this, allclasseslist, R.layout.checkin_item, new String[] { TAG_CLASSID, TAG_CLASSNAME }, new int[] { R.id.pid, R.id.name }); setListAdapter(adapter); </code></pre> <p>While I've tried this, I get a number of errors related to either defining the asyncTask wrong, or other things not matching up.</p> <p>Here is what I've tried putting in my "WebServiceTasks" class:</p> <pre><code>public static class LoadAllClasses extends AsyncTask&lt;String, String, ArrayList&lt;HashMap&lt;String, String&gt;&gt;&gt; { JSONParser jParser = new JSONParser(); ArrayList&lt;HashMap&lt;String, String&gt;&gt; allclasseslist; // JSON Node names private static final String TAG_SUCCESS = "success"; private static final String TAG_CLASSES = "classes"; private static final String TAG_CLASSID = "id"; private static final String TAG_CLASSNAME = "class"; private static final String getclasses_tag = "getclasses"; JSONArray classes2 = null; /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); // pDialog = new ProgressDialog(Checkin.this); // pDialog.setMessage("Loading products. Please wait..."); // pDialog.setIndeterminate(false); // pDialog.setCancelable(false); // pDialog.show(); } /** * getting All classes from url * */ @Override protected ArrayList&lt;HashMap&lt;String, String&gt;&gt; doInBackground( String... args) { // Building Parameters List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); params.add(new BasicNameValuePair("tag", getclasses_tag)); // getting JSON string from URL JSONObject json = jParser.makeHttpRequest(SmashGyms.WEBSERVICE_URL, "POST", params); // Check your log cat for JSON response Log.d("CheckinDialog", json.toString()); try { // Checking for SUCCESS TAG int success = json.getInt(TAG_SUCCESS); if (success == 1) { // classes found // Getting Array of Classes classes2 = json.getJSONArray(TAG_CLASSES); // looping through All Classes for (int i = 0; i &lt; classes2.length(); i++) { JSONObject c = classes2.getJSONObject(i); // Storing each json item in variable String id = c.getString(TAG_CLASSID); String name = c.getString(TAG_CLASSNAME); //String day = c.getString(TAG_DAY); // creating new HashMap HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); // adding each child node to HashMap key =&gt; value map.put(TAG_CLASSID, id); map.put(TAG_CLASSNAME, name); //map.put(TAG_DAY, day); // adding HashList to ArrayList allclasseslist.add(map); Log.d("map: ", map.toString()); } } } catch (JSONException e) { e.printStackTrace(); } return allclasseslist; } /** * After completing background task Dismiss the progress dialog * **/ @Override protected void onPostExecute( ArrayList&lt;HashMap&lt;String, String&gt;&gt; allclasses) { // dismiss the dialog after getting all products //pDialog.dismiss(); // updating UI from Background Thread } } </code></pre> <p>Is this possible, and if so, what am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    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