Android HashMap java.lang.ClassCastException:无法将java.lang.Integer强制转换为java.util.Map $ Entry

乔宾

我有一个RecyclerView包含的内容CheckBox,当被选中时应接受getAdapterPosition()ofRecyclerAdapterCheckBox作为关键字的被检查状态(布尔值),并且hashmap'.getAdapterPosition() is converted to Integer object.When theCheckBox`中的被检查时它会产生以下错误。

错误:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Map$Entry
                                                                                   at com.example.jobinsabu.destination.TourPlacesRecyclerAdapter$RecyclerHolder$1.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$1.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类)

 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));
                                }
                                 }
                       });
                    }}
0xDEADC0DE

您在这里遍历哈希图的键集:

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

该键集仅包含Integers(因为您的hashmap的键是)。Integers当然不是从Map.Entry您的衍生而来ClassCastException

您应该获取哈希映射的entryset并像这样迭代

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档