整数不能转换为java.util.HashMap [英] Integer cannot be cast to java.util.HashMap

查看:138
本文介绍了整数不能转换为java.util.HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Override在Android上使用listview时,下面的代码在给列表项目单击时出错
public void onItemClick(AdapterView<?> parent,View view,
int position,long id){

//获取列表视图大小
int size = parent.getCount();

//根据列表视图大小初始化数组
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];

//根据getItemAtPosition将数据循环到数组
for(int i = 0; i< size; i ++){
HashMap< String,String> extractor =(HashMap< String,String>)parent.getItemAtPosition(i);
//将数据填入数组
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);
//为初始视图分页器初始化添加大小
in.putExtra(size,size);
in.putExtra(QUOTES,position);
Log.v(DEBUG,position Passed:+ position);
startActivity(in);

$ / code>

显示的错误是:

  05-27 15:30:12.985:E / AndroidRuntime(23905):java.lang.ClassCastException:java.lang.Integer不能转换为java.util.HashMap 
05-27 15:30:12.985:E / AndroidRuntime(23905):at com.example.fightvaw.jsontest.MainActivity $ 4.onItemClick(MainActivity.java:226)

如何解决这类错误?

这是以前的代码在我的自定义适配器上:

  @Override 
public Object getItem(int position){
// TODO自动生成的方法存根
return(position);
}

这就解决了我的自定义适配器中的代码:

  @Override 
public Object getItem(int position){
// TODO自动生成的方法存根
返回数据获得(位置);
}

其中数据被定义为 ArrayList< HashMap< String,String>>数据;


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 ?

解决方案

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;

这篇关于整数不能转换为java.util.HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆