Nikita
Nikita

Reputation: 495

I have to remove element from arraylist on unchecking the checkbox

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.

Upvotes: 0

Views: 1064

Answers (2)

Simon Fischer
Simon Fischer

Reputation: 1196

When you remove from the ArrayList, it determines the object to be removed via the equals() method. By default, this will use object identity. The object you remove is brand new however. Thus, it won't match any element of your collection and nothing gets removed.

Override equals() in NameModel appropriately and your code will work.

Upvotes: 0

Marcelo
Marcelo

Reputation: 4608

public void onClick(View view) {
    model = new NameModel(NameList.get(position).getName());
    (...)
    CustomAdapter.nameArray.add(model);
    (...)
    CustomAdapter.nameArray.remove(model);
}

Do notice that you are always creating a new Model object inside of your onClick event. Unless your NameModel class properly implements equals() and hashcode(), the object won't be removed from the list - simply because it is not there! What you have is a list containing an object with the same name, but different memory address.

You have two alternatives:

  • Implement equals() and hashcode() on NameModel, so the Collection knows that your newly created object is "the same" as the one already there;

  • Traverse the list searching for elements with the same name and remove those.

Upvotes: 2

Related Questions