Element gets added in arraylist on checking the checkbox but doesn't get removed on unchecking it. I have to remove element from arraylist on unchecking the checkbox. I have written the following code and do let me know where I'm wrong.
public class CustomAdapter extends BaseAdapter{
NameModel model;
public static ArrayList<NameModel> nameArray;
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.item_list, null);
name = (TextView)convertView.findViewById(R.id.name);
cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
cb.setTag(position);
nameArray = new ArrayList<NameModel>();
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = (Integer)view.getTag();
CheckBox checkbox = (CheckBox)view;
model = new NameModel(NameList.get(position).getName());
model.setCheckedStatus(checkbox.isChecked());
model.setName(NameList.get(position).getName());
if(checkbox.isChecked()){
CustomAdapter.nameArray.add(model);
Toast.makeText(context, "item checked ", Toast.LENGTH_SHORT).show();
}else{
CustomAdapter.nameArray.remove(model);
Toast.makeText(context, "item unchecked ", Toast.LENGTH_SHORT).show();
}
}
});
}
return convertView;
}
I have declared ArrayList as static because it was not accessible in getView method.
On executing, it is printing both toasts as expected but not removing the element on unchecking the checkbox.
Any help on the above problem would be appreciated. Thanks in advance.