java.lang.String无法强制转换为java.util.HashMap

时间:2017-12-03 14:14:51

标签: android listview hashmap

当我运行以下程序时:

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

谁可以帮助我在不改变计划目的的情况下达到目的?

谢谢!

2 个答案:

答案 0 :(得分:0)

您正在使用ArrayAdapter<String>表示您传递了Strings的适配器对象。

现在,当您点击商品并要求parent.getItemAtPosition(position);时,您会收到Object
如果您将进入此功能,您可以看到您获得的Object是按位置和您发送到Adapter(字符串)的类型。
因此,当您尝试将该String转换为(HashMap<String, String>)时,您将获得异常。

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

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

        }
    });

答案 1 :(得分:0)

*****你能试试这段代码它可能会有所帮助。*****

公共类MainActivity扩展了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();

        }
    });
}

}