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.setAdapter(spinner);
spinner1.setSelection(0);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = null;
switch (position) {
case 1:
intent = new Intent(getApplication(), Basketball.class);
startActivity(intent);
break;
Here is how i add data to my spinner
try
{
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");
}
is.close();
result = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try
{
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");
}
list1.add("All");
for(int i = 0; i<s_name.length; i++)
{
list1.add(s_name[i] + " "+s_gender[i]);
}
} catch (JSONException e) {
e.printStackTrace();
}
spinner_fn();
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.