获取 java.lang.ClassCastException:无法强制转换 android.widget.SimpleAdapter

Posted

技术标签:

【中文标题】获取 java.lang.ClassCastException:无法强制转换 android.widget.SimpleAdapter【英文标题】:Getting java.lang.ClassCastException: android.widget.SimpleAdapter cannot be cast 【发布时间】:2019-03-11 03:16:57 【问题描述】:

我正在使用 SimpleAdapter 创建 listView,但是在单击项目时我得到了类转换异常。在 onItemClick 方法中,我试图获取我的 Customer 类的引用,此时我只获得了我可以在 android 监视器日志中看到的类转换异常.

MainActivity.java

public class MainActivity extends AppCompatActivity  

private ListView listView;
ArrayList<HashMap<String, String>> data;
SimpleAdapter adapter;
Customer customer;


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

    data=new ArrayList<>();
    data.add(new Customer("Cusomer Name1", "customer1 Name").toHasMap());
    data.add(new Customer("Cusomer Name2", "customer2 Name").toHasMap());

    String[] hasMapProperties="FirstName",              "LastName";
    int[] texfields=R.id.list_item_customer2_firstname,R.id.list_item_customer2_secndname;

    adapter=new SimpleAdapter(this,data,R.layout.list_item_customer_2,hasMapProperties,texfields);
    listView=(ListView)findViewById(R.id.main_activity_listView);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            TextView textView= (TextView) view.findViewById(R.id.list_item_customer2_firstname);

            Customer customer= (Customer) adapter.getItem(position);
            Toast.makeText(MainActivity.this,"Clicked " + customer.getFirstname() + " Postion " + position,Toast.LENGTH_SHORT).show();

        
    );

客户

public class Customer 
private final String firstname;
private final String lastname;

public Customer(String firstname, String lastname) 
    this.firstname = firstname;
    this.lastname = lastname;



public String getFirstname() 
    return firstname;


public String getLastname() 
    return lastname;


@Override
public String toString() 
    return getFirstname() + "" + getLastname();


public HashMap<String,String> toHasMap() 

    HashMap<String, String> returnValue=new HashMap<>();
    returnValue.put("FirstName",getFirstname());
    returnValue.put("LastName",getLastname());
    return  returnValue;



我是android开发的初学者

【问题讨论】:

欢迎来到 Stack Oveflow。你能把相关的logcat贴在这里吗?我的意思是,不能投到什么?如果能看到 logcat 那就太好了。 Getting java.lang.ClassCastException: android.widget.SimpleAdapter cannot be cast到什么?错误消息的最后一部分丢失。 java.lang.ClassCastException: java.util.HashMap 无法在 com.example.abhishekaryan.listviewexample2.MainActivity$1.onItemClick(MainActivity.java: 50) 在 android.widget.AdapterView.performItemClick(AdapterView.java:310) 【参考方案1】:

那是因为你的 adapter.getItem 返回一个 HashMap。像这样改变你的代码:

Map<String,String> customer= (HashMap<String, String>) adapter.getItem(position); // CHANGE CUSTOMER TYPE TO MAP

Toast.makeText(MainActivity.this,"Clicked " + customer.get("FirstName") + " Postion " + position,Toast.LENGTH_SHORT).show(); 

【讨论】:

以上是关于获取 java.lang.ClassCastException:无法强制转换 android.widget.SimpleAdapter的主要内容,如果未能解决你的问题,请参考以下文章

iOS ---------- 获取设备的各种信息

java反射获取属性值

Shell 获取路径

iOS 获取文件大小

根据日期字符串获取星期几,日期获取星期,时间获取星期,js获取星期

js如何获取时间点?