Integer cannot be cast to java.util.HashMap

2019-09-22 05:05发布

Im using listview on android, below code is giving error after clicking on a list item for another new activity

@Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // getting list view size
                int size = parent.getCount();

                // initializing array according to list view size
                String[] all_pid = new String[size];
                String[] all_name = new String[size];
                String[] all_address = new String[size];
                String[] all_latitude = new String[size];
                String[] all_longitude = new String[size];

                // looping data into array according to getItemAtPosition
                for (int i = 0; i < size; i++) {
                    HashMap<String, String> extractor = (HashMap<String, String>) parent.getItemAtPosition(i);
                    // fill data into array
                    all_pid[i] = extractor.get("pid");
                    all_name[i] = extractor.get("name");
                    all_address[i] = extractor.get("address");
                    all_latitude[i] = extractor.get("latitude");
                    all_longitude[i]=extractor.get("longitude");
                }

                Intent in = new Intent(getApplicationContext(),SingleContactActivityMainFragment.class);

                in.putExtra("APid", all_pid);
                in.putExtra("AName", all_name);
                in.putExtra("AAddress", all_address);
                in.putExtra("ALatitude", all_latitude);
                in.putExtra("ALongitude", all_longitude);
                // added size for initial view pager initialization
                in.putExtra("size", size);
                in.putExtra("QUOTES", position);
                Log.v(DEBUG, "position Passed: " + position);
                startActivity(in);
            }

Error shown is:

05-27 15:30:12.985: E/AndroidRuntime(23905): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.HashMap
05-27 15:30:12.985: E/AndroidRuntime(23905):    at com.example.fightvaw.jsontest.MainActivity$4.onItemClick(MainActivity.java:226)

how to solve these type of error ?

3条回答
你好瞎i
2楼-- · 2019-09-22 05:30

This line

HashMap<String, String> extractor = (HashMap<String, String>) parent.getItemAtPosition(i);

causes your crash. parent is an AdapterView and returns an Integer if you call getItemAtPosition(i).

You can fix this by replacing parent with your ListAdapter (which you needed to fill your ListView). Your code should look like this afterwards:

HashMap<String, String> extractor = youListAdapter.get(i);
查看更多
萌系小妹纸
3楼-- · 2019-09-22 05:42

Looks like your parent AdapterView is designed for holding int values, they will be boxed automatically to Integer. That is why by calling parent.getItemAtPosition(i) you get Integer. Obviously, Integer cannot be casted to HashMap

查看更多
等我变得足够好
4楼-- · 2019-09-22 05:43

this is previous code on my custom adapter:

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return (position);
}

and this is solved code in my custom adapter:

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return data.get(position);
}

in which datais defined as ArrayList<HashMap<String, String>> data;

查看更多
登录 后发表回答