简体   繁体   English

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

[英]Getting java.lang.ClassCastException: android.widget.SimpleAdapter cannot be cast

I am creating listView using SimpleAdapter but while clicking on items I am getting class cast exception.In onItemClick method I am trying to get reference of my Customer class at that point only getting class cast exception which I can see in Android monitor log. 我正在使用SimpleAdapter创建listView,但是在单击项目时正在获取类强制转换异常。在onItemClick方法中,我试图在那时获取我的Customer类的引用,但只能获取我可以在Android监视器日志中看到的类强制转换异常。

MainActivity.java 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();

        }
    });
}

Customer 顾客

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;

}
}

I am beginner in android development 我是android开发的初学者

That is because your adapter.getItem returns a HashMap. 那是因为您的adapter.getItem返回一个HashMap。 Change you code like this: 像这样更改您的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:无法转换android.widget.TextView - java.lang.ClassCastException: android.widget.TextView cannot be cast 获取java.lang.ClassCastException:android.widget.FrameLayout $ LayoutParams无法强制转换为android.widget.RelativeLayout $ LayoutParams - Getting java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 我不断收到错误 java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView - i keep getting the error java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView Android java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView - Android java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText - Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.FrameLayout - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException: android.widget.RelativeLayout 不能转换为 android.widget.Button - java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.EditText - java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.LinearLayout无法转换为android.widget.ScrollView - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ScrollView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM