A.Agaty A.Agaty - 5 months ago 27
Java Question

Checkbox holder using setOnCheckedChangeListener

I'm trying to make a listview with checkboxes, I have tried a code and it worked correctly but I faced a problem when trying to handle the checkbox states. The problem appears in this line of code

holder.chkBox.setOnCheckedChangeListener((MyActivity) context);


because the code I have tried is using Adapter.java file separated from the MainActivity.java file, but in my code all are in one file so I don't know how to use this line in my code, I have tried this

holder.chkBox.setOnCheckedChangeListener((this) context);


but also a wrong statement

The reason of using this line of holder is that when I checked a checkbox and scroll down through the others items, the checked checkbox is automatically unchecked, Could anyone tell me how to fix this problem?

Here is my Adapter Code

@Override public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
View itemView = convertView;
if (itemView == null){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.item, null);
holder = new ViewHolder();
holder.textView1 = (TextView) itemView.findViewById(R.id.textView1);
holder.check1 = (CheckBox) itemView.findViewById(R.id.check1);
holder.check2 = (CheckBox) itemView.findViewById(R.id.check2);
itemView.setTag(holder);
}
else{
holder = (ViewHolder) itemView.getTag();
}
holder.check1.setTag(position);
final Item item = items.get(position);
holder.textView1.setText(item.getName());
holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(buttonView.isPressed()){

if (position != ListView.INVALID_POSITION) {
Item p = items.get(position);
p.setSelected(isChecked);
}

}
}
}
);
holder.check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(buttonView.isPressed()){
if (position != ListView.INVALID_POSITION) {
Item p = items.get(position);
p.setSelected(isChecked);
}

}
}
}
);
holder.check1.setChecked(item.isSelected());
holder.check2.setChecked(item.isSelected());
return itemView;
}
}private static class ViewHolder {
public TextView textView1;
public CheckBox check2,check1;
}}

Answer

First, make your activity implement OnCheckedChangedListener interface, which forces to implement the following method :

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

        }

Now pass the reference of your activity to the your adapter as an argument of type OnCheckedChangeListener and name it listener :

MyAdapter myadapter = new MyAdapter(context, MyActivity.this,listdata);

Then, in your adapter class, set the listener for your checkbox like :

holder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {


            if(buttonView.isPressed()){
            //Write the logic to determine if the checkbox must be checked or not                
                }
          }
      }
    ); 

The point here is using the isPressed() method of the checkbox.

Comments