【问题标题】:java.lang.String cannot be cast to java.util.HashMapjava.lang.String 不能转换为 java.util.HashMap
【发布时间】:2018-05-17 02:02:53
【问题描述】:

当我运行以下程序时:

ArrayList<HashMap<String, String>> AL = new ArrayList<HashMap<String, String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_outaccountinfo);

    ListView lv = (ListView) findViewById(R.id.lvoutinfo);
    ArrayList<HashMap<String, String>> AL = new ArrayList<HashMap<String, String>>();
    DBOpen open = new DBOpen(Outaccountinfo.this);
    SQLiteDatabase db = open.getWritableDatabase();
    Cursor cursor = db.query("outaccount", null, null, null, null, null, null, null);
    while (cursor.moveToNext()) {
        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> map = new HashMap<String, String>();
                String str_id = cursor.getString(cursor.getColumnIndex("id"));
                String str_money = cursor.getString(cursor.getColumnIndex("outmoney"));
                String str_time = cursor.getString(cursor.getColumnIndex("time"));
                String str_type = cursor.getString(cursor.getColumnIndex("type"));
                String str_mark = cursor.getString(cursor.getColumnIndex("mark"));
                map.put("id", str_id);
                map.put("outmoney", str_money);
                map.put("time", str_time);
                map.put("type", str_type);
                map.put("mark", str_mark);
                AL.add(map);
            } while (cursor.moveToNext());

        }
    }
    String[] str = new String[cursor.getCount()];
    for (int i = 0; i < cursor.getCount(); i++) {
        str[i] = AL.get(i).get("id").toString() + "|" + "  " + "money:" + String.valueOf(AL.get(i).get("outmoney"))
                + "  " + "time:" + AL.get(i).get("time").toString() + "  " + "type:" + AL.get(i).get("type").toString()
                + "  " + "mark:" + AL.get(i).get("mark").toString();
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                str);
        lv.setAdapter(arrayAdapter);
    }
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            HashMap<String, String> map = (HashMap<String, String>) parent.getItemAtPosition(position);
            Toast.makeText(Outaccountinfo.this, map.get("type"), Toast.LENGTH_SHORT).show();

        }
    });
}

我收到以下错误:

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

谁能在不改变程序目的的情况下帮助我实现目的?

谢谢!

【问题讨论】:

  • 你在哪一行得到了异常?
  • @tts zhng 我的答案的任何更新
  • 在此您将创建一个字符串数组并添加到适配器。获取数据时,它将返回字符串(在 OnItemClick 中)而不是哈希图。

标签: android listview hashmap


【解决方案1】:

*****你能试试这个代码吗?它可能会有所帮助。*****

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView lv = (ListView) findViewById(R.id.lvoutinfo);
    ArrayList<HashMap<String, String>> AL = new ArrayList<HashMap<String, String>>();

   SQLiteDatabase db = openOrCreateDatabase("Account",MODE_PRIVATE,null);
    db.execSQL("CREATE TABLE IF NOT EXISTS outaccount(id text,outmoney text,time text,type text,mark text);");
    db.execSQL("INSERT INTO outaccount VALUES('1','100','10','saving','80');");
    Cursor cursor = db.query("outaccount", null, null, null, null, null, null, null);
    while (cursor.moveToNext()) {
        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> map = new HashMap<String, String>();
                String str_id = cursor.getString(cursor.getColumnIndex("id"));
                String str_money = cursor.getString(cursor.getColumnIndex("outmoney"));
                String str_time = cursor.getString(cursor.getColumnIndex("time"));
                String str_type = cursor.getString(cursor.getColumnIndex("type"));
                String str_mark = cursor.getString(cursor.getColumnIndex("mark"));
                map.put("id", str_id);
                map.put("outmoney", str_money);
                map.put("time", str_time);
                map.put("type", str_type);
                map.put("mark", str_mark);
                AL.add(map);
            } while (cursor.moveToNext());

        }
    }
    String[] str = new String[cursor.getCount()];
    for (int i = 0; i < cursor.getCount(); i++) {
        str[i] = AL.get(i).get("id").toString() + "|" + "  " + "money:" + String.valueOf(AL.get(i).get("outmoney"))
                + "  " + "time:" + AL.get(i).get("time").toString() + "  " + "type:" + AL.get(i).get("type").toString()
                + "  " + "mark:" + AL.get(i).get("mark").toString();
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                str);
        lv.setAdapter(arrayAdapter);
    }
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            HashMap<String, String> map = (HashMap<String, String>) parent.getItemAtPosition(position);
            Toast.makeText(MainActivity.this, map.get("type"), Toast.LENGTH_SHORT).show();

        }
    });
}
}

【讨论】:

  • 如果你包含几句话来描述它修复了什么以及为什么,这个答案会更好。
【解决方案2】:

你使用ArrayAdapter&lt;String&gt;表示你传递你的适配器对象的Strings。

现在,当您单击您的项目并要求输入 parent.getItemAtPosition(position); 时,您会得到一个 Object
如果你进入这个函数,你会看到你得到的Object是按位置和你发送到Adapter(字符串)的类型。
因此,当您尝试将该字符串强制转换为 (HashMap&lt;String, String&gt;) 时,您会遇到异常。

点击后尝试做类似的事情:

    HashMap<String, String> map = new HashMap<String, String>(); // Parameter in outside the click listener

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            map.put("someKey", parent.getItemAtPosition(position));
            Toast.makeText(Outaccountinfo.this, map.get("type"), Toast.LENGTH_SHORT).show();

        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    相关资源
    最近更新 更多