OK, I'll walk through your sample code and show where you're running into issues, and suggest how you can get it to work.
ArrayList<HashMap<String,String>> keys = appPrefs.getDownloadUrls();
This (above) is fine - but remember keys is an ArrayList. It's a list of HashMap objects, but it's still a list
ArrayList<String> urls = new ArrayList<String>();
ArrayList<String> filenames = new ArrayList<String>();
These are good, but in typical Java, it would be better to have List<String> urls = new ArrayList<String>(); to try and keep your variables using interfaces instead of concrete implementations.
Iterator myIterator = keys.keySet().iterator();
while(myIterator.hasNext()) {
This won't work, because keys is an ArrayList, and a list does not have a keySet() you want to do:
Iterator<HashMap<String,String> listIterator = keys.iterator();
while(listIterator.hasNext()) {
HashMap<String,String> map = listIterator.next();
Iterator<String> myIterator = map.keySet().iterator();
while(myIterator.hasNext()) {
Or, even better would be to use the Java 1.5 for(each) loop:
for( Map<String,String> map : keys ) {
for( String url : map.keySet() ) {
--
urls.add((String)myIterator.next());
The above would work, once you get myIterator to be an iterator over the map, rather than the list.
filenames.add((String)keys.get(myIterator.next()));
But this won't for 2 reasons
- Because
keys is still a list.
- If you call
next on an iterator twice then you get 2 different objects.
You need to have:
String url = myIterator.next();
urls.add(url);
filenames.add(map.get(url));
Or, if you use the for(each) loop I suggested above, then you can skip that first line.
Hope that helps - if something's unclear please add a comment.
Note: solilo's solution is a lot simpler and is a good way to do it, my answer is here to help you see where you were running into trouble.