Skip to content Skip to sidebar Skip to footer

How To Pass Custom Object With Checked Checkbox Through Intent In Adapter To Activity

I am displaying hotels in listview with checkbox,image and text. when user click on checkbox then put that checked hotel_id into bundle. And pass to it's super class. When i am sel

Solution 1:

The problem is you are only putting data to intent. It is just worth if You put bundle or data inside intent object with using startActivity.

If you want to get selected Hotel id's just make a public array like:-

publicclassHotelsextendsActivity{
 public ArrayList<Integer> hotel_ids=new ArrayList<>();
}

and inside adapter while selecting check box put id in arrayList with the use of context as below and if unchecked remove hotel_id from that array list :-

 c.setOnClickListener(new View.OnClickListener() {
            publicvoidonClick(View v) {

                if (c.isChecked() && checkCounter >= 3) {
                    AllMenu.get(position).setSelected(false);         
                    c.setChecked(false);
                    Toast.makeText(context, "You can select max 3 hotels!!", Toast.LENGTH_SHORT).show();
                } else {
                    Product p = (AllMenu).get(position);
                    p.setSelected(c.isChecked());
                    if (c.isChecked()) {
                       if(context instanceof Hotels){
           ((Hotels)context).hotel_ids.add(p.getId());
                        }  
                        checkCounter++;

                    } else {
                       if(context instanceof Hotels){
               ((Hotels)context).hotel_ids.remove(p.getId());
                       }  
                        checkCounter--;
                    }

                }
    }
}

It may help you out. With the above one you will always find selected Hotel_ids.

Solution 2:

you can do something like this.

1> in getview method you can do checked listener like below

  viewHolder.chkbox.setOnCheckedChangeListener(new CheckUpdateListener(itemList.get(position));

2> make new class for CheckUpdatelistener with argument constructor of model class

privatefinalclassCheckUpdateListenerimplementsCompoundButton.OnCheckedChangeListener {
    privatefinal Product product;

    privateCheckUpdateListener(Product product) {
        this.product = product;


    }


    publicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.i("onCheckedChanged", "isChecked: " + isChecked);
        product.setChecked(isChecked);


        notifyDataSetChanged();




    }
}

3> in main activity on button click listener you can do code like this

     Arraylist<Product> selectedProduct = new ArrayList<>();




            if (itemList.size() > 0) {
                for (int i = 0; i < itemList.size(); i++) {
                    if (itemList.get(i).isChecked()) {
                        try {

                            selectedProduct.add(itemList.get(i));
                        } catch (NullPointerException e) {
                            e.printStackTrace();
                        }
                    }
                }

Post a Comment for "How To Pass Custom Object With Checked Checkbox Through Intent In Adapter To Activity"