如何将我的微调器ArrayAdapter <string>更改为hashmap?

时间:2017-02-07 12:23:09

标签: android spinner

如何将我的微调器从字符串更改为hashmap?我使用json从我的数据库中获取我的数据。我想在每次选择一个微调器数据时传递id。问题是我不知道如何将我的微调器更改为hashmap。帮帮我吧谢谢!

这是我的微调器

 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;

以下是我如何将数据添加到我的微调器

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();


    }

1 个答案:

答案 0 :(得分:0)

要使用带有arrayadapter的hashmap,您需要带有hashmap类型的列表:

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

并将此列表传递给您的适配器:

ArrayAdapter<Object> spinner = new ArrayAdapter<>(Games.this, layout.simple_spinner_dropdown_item, list);
希望这对你有所帮助。