CheckBox is automatically getting deselected while I scroll down Here is my following code:
package com.serial.teach;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class TeachAdapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> roll =new ArrayList<String>();
ArrayList<String> checkbox=new ArrayList<String>();
private int lastPosition = -1;
private int counter = 0;
ArrayList<Boolean> status = new ArrayList<Boolean>();
public static List<Integer> SelectedBox = new ArrayList<Integer>();
Animation animation;
View row;
public TeachAdapter(Context c,ArrayList<String> r, ArrayList<String> ch) {
super(c, R.layout.single_rows, R.id.name, ch);
// TODO Auto-generated constructor stub
this.context = c;
this.roll = r;
this.checkbox = ch;
for (int i = 0; i < roll.size(); i++) {
status.add(false);
}
}
class MyViewHolder {
TextView tv;
CheckBox cb;
public MyViewHolder(View v) {
// TODO Auto-generated constructor stub
tv = (TextView) v.findViewById(R.id.roll);
cb = (CheckBox) v.findViewById(R.id.name);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
row = convertView;
final MyViewHolder holder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_rows, parent, false);
holder = new MyViewHolder(row);
row.setTag(holder);
} else {
holder = (MyViewHolder) row.getTag();
}
animation = AnimationUtils.loadAnimation(getContext(),
(position > lastPosition) ? R.anim.up_from_bottom
: R.anim.abc_fade_in);
//down_from_top
row.startAnimation(animation);
lastPosition = position;
holder.tv.setText(roll.get(position));
holder.cb.setText(checkbox.get(position));
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (holder.cb.isChecked()) {
status.set(position, true);
SelectedBox.add(Integer.parseInt((roll.get(position))));
Toast.makeText(getContext(), "You selected " + roll.get(position)+" "+ checkbox.get(position),
Toast.LENGTH_SHORT).show();
holder.cb.setTextColor(Color.rgb(0, 255, 0));
} else {
status.set(position, false);
Toast.makeText(getContext(), "You unchecked " + position,
Toast.LENGTH_SHORT).show();
holder.cb.setTextColor(Color.rgb(255, 0, 0));
}
}
});
holder.cb.setChecked(status.get(position));
holder.cb.setTag(position);
return row;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
}
I have a textView and a checkbox like this:
TV |_|Checkbox
TV |_|Checkbox
TV |_|Checkbox
TV |_|Checkbox
TV |_|Checkbox
But for many items when i select its deselecting automatically.
My layout consists of a textView and a CheckBox as mentioned