Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access (iterate?) an ArrayList/Hashmap in another class?
    text
    copied!<p>I have defined an ArrayList in my main class where I am loading data from an XML feed and creating various tabs with different activities:</p> <pre><code>static ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); </code></pre> <p>Example of where I put a map entry into the ArrayList of HashMaps:</p> <pre><code>for (int i = 0; i &lt; 10; i++) { HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); Element e = (Element)nodes.item(i); map.put("id", XMLfunctions.getValue(e, "id")); map.put("lat", "Lat: " + XMLfunctions.getValue(e, "lat")); map.put("long", "Long: " + XMLfunctions.getValue(e, "long")); mylist.add(map); } </code></pre> <p>The first tab is a ListViewActivity and I am successfully able to create the list by referencing the ArrayList in the main class: <em>class.mylist</em> via a List Adapter:</p> <pre><code>ListAdapter adapter = new SimpleAdapter( ListViewActivity.this, main.mylist, R.layout.list, new String[] { "id", "lat", "long" }, new int[] { R.id.item_id, R.id.item_lat, R.id.item_long }); setListAdapter(adapter); </code></pre> <p>My second tab is a MapView and I am able to successfully switch tabs, display the map within the tab layout and pan &amp; zoom etc.</p> <p>Where I need help is with understanding how to access the 'lat' and 'long' strings within the "mylist" ArrayList/Hashmap in the main class (<strong>Note</strong>: <em>I declared the ArrayList/Hashmap in the main class and then share it across all the activities because it is quite a large XML feed and I did not want to be reloading data or having too many objects in memory</em>).</p> <p>My objective is to then convert these lat/long co-ordinates into GeoPoints and display them using a Map Overlay; I am already able to do this using fixed GeoPoints hard-coded into my Map activity class. However, as I wrote above, I now need to replace these 'fixed' GeoPoints and instead use the contents of the "mylist" ArrayList/Hashmap.</p> <p>I am new (&lt;1 month experience) to programming Java/Android, so I hope this all makes sense? And I hope that someone will be able to explain / give an example of how I can access the data... I've tried implementing various measures such as '<strong>Iterations</strong>', but it's not working yet.</p>
 

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