tags:

views:

38

answers:

1

Hello, i have a hashtable filled with data, but i dont know the keys How can i loop thorugth a HashTable keys in android? Im trying this, but it doesnt work

Hashtable output=new Hashtable(); output.put("pos1","1"); output.put("pos2","2"); output.put("pos3","3");

ArrayList mykeys=(ArrayList)output.keys(); for (int i=0;i< mykeys.size();i++){
txt.append("\n"+mykeys.get(i)); }

A: 

You should use Map<String, String> instead of Hashtable, and the for-each notation for iteration whenever possible.

 Map<String, String> output = new HashMap<String, String>(); 
 output.put("pos1","1"); 
 output.put("pos2","2"); 
 output.put("pos3","3");

 for (String key : output.keySet()) {
   txt.append("\n" + key);
 }

Your current code doesn't work because Hashtable.keys() returns an Enumeration, but you try to cast it to ArrayList which is not assignable from Enumeration.

oksayt