Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a RecyclerView containing a CheckBox which when checked should accept the getAdapterPosition() of RecyclerAdapter and checked state(boolean) of CheckBox as key and value in hashmap'.getAdapterPosition() is converted to Integer object.When theCheckBox` is checked it gives the following error.

Error:

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:(Full java class not included)

 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));
                                }
                                 }
                       });
                    }}
share|improve this question
1  
Try replacing hashmap.keySet() with hashMap.entrySet(). It should fix your issue I suppose. – Bhargav Mar 2 at 7:13
up vote 1 down vote accepted

You are iterating over the keyset of the hashmap here:

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

The keyset contains only Integers (because the key of you hashmap is). Integers are, off course, not derived from Map.Entry hence your ClassCastException.

You should get the entryset of your hashmap and iterate that like this

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();
}
share|improve this answer

You have:

Set s=hashMap.keySet();

You probably meant to get the entry set instead:

Set s=hashMap.entrySet();

If you used generics the compiler would have detected this problem for you.

share|improve this answer

you should use hashMap.entrySet() instead of hashMap.keySet()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.