Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a view is used as template and id repeat, how can single checkbox be selected on click of its parent

I'm receiving a series of data from the database and displaying it in a layer that consists of a checkbox and a few other views.In fact, I've made a controlling custom. My question is how can I turn on the checkbox of that layer when clicked on any layer , and the rest are turned off.

public class myclass extends LinearLayout {

LinearLayout linear;
public int id;
public static ImageView pic;
public static TextView titlepro;
public static CheckBox rdb;

public myclass(Context context) {
    super(context);
    init(context);
}
public myclass(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init(context);

}
public myclass(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);

}
public void init(Context context){
    LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view=inflater.inflate(R.layout.linear,this,true);
    linear=(LinearLayout)findViewById(R.id.linear2);
    pic=(ImageView)view.findViewById(R.id.pic);
    titlepro=(TextView)view.findViewById(R.id.txt1);
    rdb=(CheckBox)view.findViewById(R.id.rdb1);

    rdb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){

            }
        }
    });
    linear.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            if (rdb.isChecked()){
                rdb.setChecked(false);
            }else {
                rdb.setChecked(true);
            }
            Toast.makeText(G.context,id+"",Toast.LENGTH_LONG).show();
        }
    });

}}

My problem is that when you click on a layer, the checkbox for the other layer is checked ,but return id correctly.

like image 450
mohsen khorasani Avatar asked Dec 07 '25 05:12

mohsen khorasani


1 Answers

The issue is because you are using id that is same for all checkboxes.

There can be couple of ways to solve this:

1.linear=(LinearLayout)findViewById(R.id.linear2); -> linear=(LinearLayout)view.findViewById(R.id.linear2);

if that does not work:

2.Consider creating a new fragment, that has all views in a layer. Then you may add multiple of those to your view. In side that class consider handling layer click event.

  1. Use recycler view then you will get index of item clicked and handle from there. In case you are getting entries from db and you may know how many there might be, your UI may get too heavy. Using recycler view will help with performance
like image 170
Stakshi Avatar answered Dec 09 '25 15:12

Stakshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!