为什么我收到以下错误:
java.lang.String cannot be cast to 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;
...
}
}
});
这是我的logcat:
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap
at com.example.abe.ateneofiesta.Games$1.onItemSelected(Games.java:277)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:924)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:913)
at android.widget.AdapterView.-wrap1(AdapterView.java)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:883)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
检索微调器的值
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();
}
答案 0 :(得分:0)
ArrayAdapter<String> spinner
一旦你声明了,你永远不会通过adapter.getItemAtPosition(int)获得HashMap的实例,它将返回一个instanceof String
答案 1 :(得分:0)
parent.getItemAtPosition(position)
返回String,因为您的适配器的类型为String
。如果您需要此功能,请更改为ArrayAdapter<Hashmap>
。
答案 2 :(得分:0)
您的微调器使用字符串arraylist填充..因此您无法接收Hashmap
在onItemSelected
期间
试试这个:
//Declare hashmap globally
HashMap<Integer,String> haspMap_name = new HashMap<Integer, String>();
HashMap<Integer,String> = new HashMap<Integer, String>();
现在填充Hashmap
使用:
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]);
}
在onItemSelected
期间使用..
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;
...
}