0

I have a code populating a listView:

JSONArray data = responseData.getJSONArray("data");
String[] values = new String[data.length()];//I wanna get rid of this

LinkedHashMap<String, String> helpData = new LinkedHashMap();
for (int i = 0; i < data.length() ; i++) {
  String header = data.getJSONObject(i).getString("glossary_header");
  String description = data.getJSONObject(i).getString("gloassary_description");

  helpData.put(header, description);
    values[i] = header;
  Log.d("mylog", "counter" + i);
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);

I want to pass the keys to Arrayadapter, I was hoping to find a getKeys() method that could magically return an array of key from the map.

KeySet() was close but did not work, what is the proper way to do this. I don't want to use string array. I want to have my pair values together.

  • 2
    What do you mean "keySet() did not work"? it's the correct thing to do. In what way was it inadequate for your purposes? – Dawood ibn Kareem Apr 29 '14 at 22:25
  • @DavidWallace compiler complains about the set, arrayAdapter wants an array instead –  Apr 29 '14 at 22:27
  • Is copying the contents of the set into an array acceptable? – Richard Tingle Apr 29 '14 at 22:29
  • You can use for-each loop like `for(String key: yourLinkedHashMap.keySet()){ System.out.println(key); }` – jmj Apr 29 '14 at 22:32
  • possible duplicate of [Get a HashSet out of the keys of a HashMap?](http://stackoverflow.com/questions/1625814/get-a-hashset-out-of-the-keys-of-a-hashmap) – Lily Chung Apr 29 '14 at 22:35

3 Answers3

1

You can get like this

   Collection<String> values = helpData.keySet();

     for (String string : values) {
           //
       }
Asif Bhutto
  • 3,916
  • 1
  • 24
  • 21
0
for (final String key : helpData.keySet()) {
  // print data...
}

or

final Iterator<String> cursor = helpData.keySet().iterator();
while (cursor.hasNext()) {
  final String key = cursor.next();
 // Print data
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0
Set<String> keys = myArray.keySet();
String[] keysAsArray = keys.toArray(new String[0]);

More detail on the toArray method can be found at http://docs.oracle.com/javase/7/docs/api/java/util/Set.html#toArray(T[])

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Thanks I was looking for a one-liner! now I am curious to know why you pass it `new String[0]` it works with different indexes also. can you elaborate. please –  May 01 '14 at 17:41
  • Sure. You can also write `keys.toArray()`, but it returns an `Object[]`, because there is no way the Java runtime can know what type of array to make. There's this thing called "type erasure", which basically means that whatever's written inside the angle brackets gets removed by the compiler. So the way to deal with that is to pass a dummy array to `toArray` to tell it what type of array to make. If you pass in a large enough array, this method will just populate the array. If your array is too small, this method will build a new one of the right type. – Dawood ibn Kareem May 01 '14 at 19:13
  • This makes sense,Thanks I learned something, veryuseful. One more question if you don't mind. Am I doing this right. I need pair values because I want to display the details in another view. Is my code a good start? –  May 01 '14 at 22:13
  • If you need pairs (I guess you mean the key and the value together), then why did you ask for just the keys? Can't the "other view" you speak of just accept the `LinkedHashMap`? – Dawood ibn Kareem May 01 '14 at 22:17
  • Because I display the keys in my listView then If pressed I wanna show the values. make sense? –  May 01 '14 at 22:21
  • Jenny, I'm not an Android developer, so I'm not particular familiar with `ListView` and what you need to pass to it. http://www.mkyong.com/android/android-listview-example/ looks like a nice tutorial. Does it make sense? If the answer is no, I'll try again to help you. – Dawood ibn Kareem May 01 '14 at 22:40