Question

I have a method in a client class that gathers data from a server, and returns a HashMap. I want to list the keys in an android dialog. The android API requires that the data must be in in the form of a CharSequence[]. So far as I can tell, there is no direct way to convert these data types. What would be the best way to accomplish this?

Here is the declaration and initialization of the hashmap and keyset:

Map <String, String> players = client.listPlayers();
Set<String> player_names = players.keySet();

And here is the link to the android Dialog documentation.

Note: CharSequence, which is able to be casted from a Set is not the same as CharSequence[]

Any help is appreciated.

-Sean W.

Was it helpful?

Solution

CharSequence[] player_names = players.keySet().toArray(new CharSequence[0]);

This tells the method the type of the array without allocating any space for elements. It will allocate a correctly-sized array for you.

If you prefer to allocate the real array yourself:

CharSequence[] player_names = players.keySet().toArray(new CharSequence[players.size()]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top