Android HashMap java.lang.ClassCastException: java.lang.Integer 无法转换为 java.util.Map$Entry
Android HashMap java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Map$Entry
我有一个 RecyclerView
包含一个 CheckBox
,当检查时应该接受 RecyclerAdapter
的 getAdapterPosition()
和 CheckBox
的检查状态(布尔值)作为检查 hashmap'.getAdapterPosition() is converted to Integer object.When the
CheckBox` 中的键和值会出现以下错误。
错误:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Map$Entry
at com.example.jobinsabu.destination.TourPlacesRecyclerAdapter$RecyclerHolder.onChange(TourPlacesRecyclerAdapter.java:89)
at com.github.lguipeng.library.animcheckbox.AnimCheckBox.setChecked(AnimCheckBox.java:291)
at com.github.lguipeng.library.animcheckbox.AnimCheckBox.setChecked(AnimCheckBox.java:264)
at com.github.lguipeng.library.animcheckbox.AnimCheckBox.onClick(AnimCheckBox.java:74)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
TourPlacesRecyclerAdapter.java:(完整 java class 不包括)
public class RecyclerHolder extends RecyclerView.ViewHolder{
HashMap<Integer,Boolean> hashMap;
AnimCheckBox checkBox;
public RecyclerHolder(final View itemView) {
super(itemView);
checkBox=(AnimCheckBox) itemView.findViewById(R.id.checkBox);
checkBox.setChecked(false);
checkBox.setOnCheckedChangeListener(new AnimCheckBox.OnCheckedChangeListener() {
@Override
public void onChange(boolean checked) {
if(checkBox.isChecked()){
boolean check=true;
clicked_position=getAdapterPosition();
i=new Integer(clicked_position);
hashMap.put(i, check);
Toast.makeText(context,"You checked item:"+getAdapterPosition(),Toast.LENGTH_SHORT).show();
Set s=hashMap.keySet();
Iterator i=s.iterator();
while (i.hasNext()){
Map.Entry entry=(Map.Entry)i.next();
Log.e("Position"+entry.getKey().toString(),"Status"+entry.getValue().toString());
}
SharedPreferences sharedPreferences=context.getSharedPreferences("Main",Context.MODE_PRIVATE);
bt_click_status=sharedPreferences.getBoolean("Btnheck",false);
Log.e("Entered",Boolean.toString(bt_click_status));
}
}
});
}}
您正在此处迭代散列映射的键集:
Set s=hashMap.keySet();
Iterator i=s.iterator();
while (i.hasNext()){
Map.Entry entry=(Map.Entry)i.next();
Log.e("Position"+entry.getKey().toString(),"Status"+entry.getValue().toString());
}
keyset只包含Integer
s(因为你的hashmap的key是)。当然,Integer
不是源自 Map.Entry
,因此您的 ClassCastException
。
您应该获取哈希图的条目集并像这样迭代
Set<Map.Entry<Integer, Boolean>> s = map.entrySet();
Iterator<Map.Entry<Integer, Boolean>> i = s.iterator();
while (i.hasNext()) {
Map.Entry<Integer, Boolean> entry = (Map.Entry<Integer, Boolean>) i.next();
}
你有:
Set s=hashMap.keySet();
您可能打算获取条目集:
Set s=hashMap.entrySet();
如果您使用泛型,编译器会为您检测到这个问题。
你应该使用 hashMap.entrySet()
而不是 hashMap.keySet()
我有一个 RecyclerView
包含一个 CheckBox
,当检查时应该接受 RecyclerAdapter
的 getAdapterPosition()
和 CheckBox
的检查状态(布尔值)作为检查 hashmap'.getAdapterPosition() is converted to Integer object.When the
CheckBox` 中的键和值会出现以下错误。
错误:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Map$Entry
at com.example.jobinsabu.destination.TourPlacesRecyclerAdapter$RecyclerHolder.onChange(TourPlacesRecyclerAdapter.java:89)
at com.github.lguipeng.library.animcheckbox.AnimCheckBox.setChecked(AnimCheckBox.java:291)
at com.github.lguipeng.library.animcheckbox.AnimCheckBox.setChecked(AnimCheckBox.java:264)
at com.github.lguipeng.library.animcheckbox.AnimCheckBox.onClick(AnimCheckBox.java:74)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
TourPlacesRecyclerAdapter.java:(完整 java class 不包括)
public class RecyclerHolder extends RecyclerView.ViewHolder{
HashMap<Integer,Boolean> hashMap;
AnimCheckBox checkBox;
public RecyclerHolder(final View itemView) {
super(itemView);
checkBox=(AnimCheckBox) itemView.findViewById(R.id.checkBox);
checkBox.setChecked(false);
checkBox.setOnCheckedChangeListener(new AnimCheckBox.OnCheckedChangeListener() {
@Override
public void onChange(boolean checked) {
if(checkBox.isChecked()){
boolean check=true;
clicked_position=getAdapterPosition();
i=new Integer(clicked_position);
hashMap.put(i, check);
Toast.makeText(context,"You checked item:"+getAdapterPosition(),Toast.LENGTH_SHORT).show();
Set s=hashMap.keySet();
Iterator i=s.iterator();
while (i.hasNext()){
Map.Entry entry=(Map.Entry)i.next();
Log.e("Position"+entry.getKey().toString(),"Status"+entry.getValue().toString());
}
SharedPreferences sharedPreferences=context.getSharedPreferences("Main",Context.MODE_PRIVATE);
bt_click_status=sharedPreferences.getBoolean("Btnheck",false);
Log.e("Entered",Boolean.toString(bt_click_status));
}
}
});
}}
您正在此处迭代散列映射的键集:
Set s=hashMap.keySet();
Iterator i=s.iterator();
while (i.hasNext()){
Map.Entry entry=(Map.Entry)i.next();
Log.e("Position"+entry.getKey().toString(),"Status"+entry.getValue().toString());
}
keyset只包含Integer
s(因为你的hashmap的key是)。当然,Integer
不是源自 Map.Entry
,因此您的 ClassCastException
。
您应该获取哈希图的条目集并像这样迭代
Set<Map.Entry<Integer, Boolean>> s = map.entrySet();
Iterator<Map.Entry<Integer, Boolean>> i = s.iterator();
while (i.hasNext()) {
Map.Entry<Integer, Boolean> entry = (Map.Entry<Integer, Boolean>) i.next();
}
你有:
Set s=hashMap.keySet();
您可能打算获取条目集:
Set s=hashMap.entrySet();
如果您使用泛型,编译器会为您检测到这个问题。
你应该使用 hashMap.entrySet()
而不是 hashMap.keySet()