0

I am trying to pass data from adapter to activity through bundle.

I am checking a checkbox

            checkboxSelectSubCategory.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    productModelsAL.get(getAdapterPosition()).setCategorySelected(true);
                }else {
                    productModelsAL.get(getAdapterPosition()).setCategorySelected(false);
                }
            }
        });

and setting data correctly in the object as I have debugged:

   Intent intent = new Intent(mContext, SelectProductActivity.class);
   Bundle bundle = new Bundle();
   bundle.putParcelable("productModel", productModelsAL.get(getAdapterPosition()));
   intent.putExtras(bundle);
   mContext.startActivity(intent);

But in activity I am not receiving the boolean value which was passed. in activitie's onCreate:

    Intent intent = getIntent();
    if (intent != null) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            subCategory = (SubCategory) bundle.getParcelable("productModel");
        }
    }
    allProductsSelected = subCategory.isCategorySelected();

Why I am not getting the value I am passing??

Makarand
  • 983
  • 9
  • 27
  • Please accept your answer as that will remove the question from the list of unaanswered questions and maybe help someone else with a similar problem. – David Wasser Dec 10 '19 at 16:52

1 Answers1

0

Actually, my Model class was incorrect.

I had model class n all implemented correctly first.

But for the extra functionality I included the checkbox in the model and created setter getters. But didn't updated the required methods for Parcelable.

Solution:

I deleted the three auto generated methods for Parcelable.

  1. Constructor
  2. Creator
  3. writeToParcel

and recreated them with Alt+Enter.

That's it.

It was bit ignorant and I wasted lot of time with it. Just thought it might help somebody.

Makarand
  • 983
  • 9
  • 27
  • 1
    You could also do it without using the bundle, intents have a method for putExtra that takes a parcelable i.e putExtra("productModel", myParcelableObject), then get it put the other side with getIntentI().getParcelableExtra(...); – MichaelStoddart Dec 06 '19 at 08:21