Reputation: 70
I want to display each key-value pair of a hashmap in a multi column ListView. I created a custom adapter for the listview but I'm not able to understand how I can retrieve the values of all the keys.
MyAdapter.java:
public final class MyAdapter {
private Activity activity;
private HashMap<String, String> map;
public MyAdapter(Activity activity, HashMap<String, String> map) {
this.activity = activity;
this.map = map;
}
public int getCount() {
return map.size();
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.my_list_item,
null);
}
String key = //GET THE KEY VALUE HERE
String value = (String) map.get(key);
TextView keyView = (TextView) convertView.findViewById(R.id.item_key);
keyView.setText(key);
TextView valV = (TextView) convertView.findViewById(R.id.item_value);
valV.setText(value);
return convertView;
}
public void setItemList(HashMap<String, String> map) {
this.map = map;
}
}
Upvotes: 0
Views: 3881
Reputation: 2906
In your constructor get the entrySet and create an array from it:
Map.Entry<String, String>[] entryArray = map.entrySet().toArray(new Map.Entry<String, String>[0]);
Then in getView:
String key = entryArray[position].getKey();
String value = entryArray[position].getValue();
Upvotes: 0
Reputation: 6620
There you go.
You can retrieve HashMap Keys using: myMap.keySet()
function, like this:
Set<String> = map.keySet();
Or, you can iterate through the HashMap like this:
public static void iterateMap(Map map) {
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry item = (Map.Entry)iterator.next();
//Do whatever you like with item.getKey() and item.getValue()
iterator.remove();
}
}
Finally, you can use map.entrySet() function:
for (Map.Entry<String, String> item : map.entrySet()){
//Do as you please with item.getKey() and itme.getValue()
}
Upvotes: 0
Reputation: 1254
A Map doesn't really work that way, you cant guarantee that if you iterate over items that they will always be in the same order, you should consider using a list instead.
You can, however, iterate over all items in a map like this:
for (Map.Entry<String, String> entry : map.entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
But note, that they might not, and probably will not always be in the same order that you put them in. Maps are designed to store values that you get by keys and not by indexes (like you would with lists or arrays).
@Campiador's solution will provide you all the keys but you still wont be able to reference them by index.
Another solution might be to iterate over all items in the map (check above for loop) and store the keys in a list, then use that for your ListView
, and pull the values from the map every time in the getView
function by using something like this:
map.get(myList.get(position));
Upvotes: 1