【发布时间】:2017-02-08 20:46:52
【问题描述】:
当我在微调器上选择数据时,出现错误 java.lang.String cannot be cast to java.util.HashMap。我从我的数据库中检索我的微调器中的数据。我试图只有 1 个类,当我在我的微调器上选择一个项目时,它会检查我的数据库中的 id 以显示过滤器并只显示选定的 id
这是我的微调器选择
ArrayList<HashMap<String,String>> list1 = new ArrayList<>();
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();
ArrayList<HashMap<String,String>> spinner = new ArrayList<Object>(Games.this, layout.simple_spinner_dropdown_item, s_name);
spinner1.setAdapter((SpinnerAdapter) 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<Object, Object> map = (HashMap)parent.getItemAtPosition(position);
String news_id = map.get(Config.TAG_news_id).toString();
String title = map.get(Config.TAG_title).toString();
String content = map.get(Config.TAG_content).toString();
String n_date = map.get(Config.TAG_n_date).toString();
intent.putExtra(Config.News_id,news_id);
intent.putExtra(Config.Title,title);
intent.putExtra(Config.Content,content);
intent.putExtra(Config.N_date,n_date);
startActivity(intent);
break;
我在这里收到错误,说在 arraylist 中不能应用于 java.lang.string
(Games.this, layout.simple_spinner_dropdown_item, s_name);
list1.add("All");
for(int i = 0; i<s_name.length; i++)
{
list1.add(s_name[i] + " "+s_gender[i]);
}
【问题讨论】:
-
阅读文档。
getItemAtPosition(position)不返回 HashMap,它返回一个字符串。 HashMap 和 String 是完全不同的类,不能将 String 转换为 HashMap。(HashMap)parent.getItemAtPosition(position);将不起作用。您的list1是HashMaps 的数组列表,而不是Strings。您不能将字符串作为项目添加到list1,您需要添加HashMap