Java 无法在微调器项选择上强制转换字符串HashMap

Java 无法在微调器项选择上强制转换字符串HashMap,java,android,android-spinner,Java,Android,Android Spinner,为什么会出现以下错误: java.lang.String不能强制转换为java.util.HashMap 当我选择微调器值时 这是我的密码: ArrayAdapter<String> spinner = new ArrayAdapter<String>(Games.this, layout.simple_spinner_dropdown_item, list1); spinner1.setAdapter(spinner); spinner1.setSelection(0)

为什么会出现以下错误:

java.lang.String不能强制转换为java.util.HashMap

当我选择微调器值时

这是我的密码:

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);
                HashMap map =(HashMap)parent.getItemAtPosition(position);
                String s_id = map.get(Config.TAG_s_id).toString();
                String s_name = map.get(Config.TAG_s_name).toString();
                String s_gender = map.get(Config.TAG_s_gender).toString();
                intent.putExtra(Config.s_id,s_id);
                intent.putExtra(Config.s_name,s_name);
                intent.putExtra(Config.s_gender,s_gender);
                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();

    }
试试看
{
URL=新URL(Config.URL\u SPIN);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
is=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(即“UTF-8”);
StringBuilder sb=新的StringBuilder();
而((line=bufferedReader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
尝试
{
JSONArray JA=新JSONArray(结果);
JSONObject json;
s_name=新字符串[JA.length()];
s_gender=新字符串[JA.length()];
对于(int i=0;i
ArrayAdapter微调器

一旦声明,就永远不会通过adapter.getItemAtPosition(int)获取HashMap实例,它将返回一个instanceof字符串
返回字符串,因为您的适配器类型为
String
。如果需要此功能,请更改为
ArrayAdapter

您的微调器是使用字符串arraylist填充的。因此,您无法接收
Hashmap
在<代码>期间,已选择<代码>

试试这个:

//Declare hashmap globally
HashMap<Integer,String> haspMap_name = new HashMap<Integer, String>();
HashMap<Integer,String>  = new HashMap<Integer, String>();
并在
过程中选择
使用..检索值

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = null;
    switch (position) {
        case 1:
            intent = new Intent(getApplication(), Basketball.class);

            String spinner_name = parent.getItemAtPosition(position).toString();
            String s_name = haspMap_name.get(position);
            String s_gender = haspMap_gender.get(position);

            //intent.putExtra(Config.s_id,s_id);
            intent.putExtra(Config.s_name,s_name);
            intent.putExtra(Config.s_gender,s_gender);
            startActivity(intent);
            break;
            ...
     }
public void已选择(AdapterView父视图、视图视图、int位置、长id){
意向=无效;
开关(位置){
案例1:
intent=newintent(getApplication(),Basketball.class);
字符串微调器_name=parent.getItemAtPosition(position.toString();
字符串s_name=haspMap_name.get(位置);
字符串s_gender=haspMap_gender.get(位置);
//intent.putExtra(Config.s_id,s_id);
intent.putExtra(Config.s_name,s_name);
intent.putExtra(配置s_性别,s_性别);
星触觉(意向);
打破
...
}

因为它不是一个
HashMap
。您如何在
列表1
中添加值?@rafsanahmad007添加了关于我如何添加值的代码sirI在这里有一个错误sir String spinner\u name=parent.getItemAtPosition(position);表示必需的java.lang.stringlist1.add(s_name[i]+“”+s_gender[i]);此处表示无法解析的方法java.stringuse
String spinner\u name=parent.getItemAtPosition(position.toString();
list1.add(s_name[i]+“”+s_性别[i])
您以前的代码中存在此错误。不起作用,先生。list1.add中仍有错误。添加部分是错误。添加部分在您以前的代码中。无需修改它
//Declare hashmap globally
HashMap<Integer,String> haspMap_name = new HashMap<Integer, String>();
HashMap<Integer,String>  = new HashMap<Integer, String>();
 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");
    haspMap_gender.put(i,s_gender[i]);
    haspMap_name.put(i,s_name[i]);
}

for(int i = 0; i<s_name.length; i++)
{
    list1.add(s_name[i] + " "+s_gender[i]);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = null;
    switch (position) {
        case 1:
            intent = new Intent(getApplication(), Basketball.class);

            String spinner_name = parent.getItemAtPosition(position).toString();
            String s_name = haspMap_name.get(position);
            String s_gender = haspMap_gender.get(position);

            //intent.putExtra(Config.s_id,s_id);
            intent.putExtra(Config.s_name,s_name);
            intent.putExtra(Config.s_gender,s_gender);
            startActivity(intent);
            break;
            ...
     }