Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

android - Integer cannot be cast to java.util.HashMap

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...