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[])
Frage
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.
Lösung 2
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[])
Andere Tipps
You can get like this
Collection<String> values = helpData.keySet();
for (String string : values) {
//
}
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
}