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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With