osoda osoda - 1 year ago 117
Android Question

How do i change my spinner ArrayAdapter<String> into a hashmap?

How do i change my spinner from string into a hashmap? I am getting my data in spinner from my database using json. I want to pass by id everytime a select a spinner data. The problem is i dont know how to change my spinner into a hashmap. Help me pls. Thank you!

Here is my spinner

final List<String> list1 = new ArrayList<String>();
spinner1 = (Spinner) findViewById(R.id.sp1);

ArrayAdapter<String> spinner = new ArrayAdapter<String>(Games.this, layout.simple_spinner_dropdown_item, list1);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = null;
switch (position) {
case 1:
intent = new Intent(getApplication(), Basketball.class);

Here is how i add data to my spinner

URL url = new URL (Config.URL_SPIN);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
is = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
while((line = bufferedReader.readLine()) != null)
sb.append(line + "\n");
result = sb.toString();
} catch (MalformedURLException e) {
} catch (IOException e) {

JSONArray JA = new JSONArray(result);
JSONObject json;

s_name = new String[JA.length()];
s_gender = new String[JA.length()];

for(int i = 0; i<JA.length(); i++)
json = JA.getJSONObject(i);
s_gender[i] = json.getString("s_gender");
s_name[i] = json.getString("s_name");
for(int i = 0; i<s_name.length; i++)
list1.add(s_name[i] + " "+s_gender[i]);

} catch (JSONException e) {


Answer Source

To use hashmap with arrayadapter you need list with hashmap type:

List<Object> list = new ArrayList<>();

And pass this list to your adapter:

ArrayAdapter<Object> spinner = new ArrayAdapter<>(Games.this, layout.simple_spinner_dropdown_item, list);

hope this may helps you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download