0

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.

Nikita
  • 425
  • 1
  • 7
  • 19
  • How can you remove a non-existing item from your list ? – Don Chakkappan Sep 17 '15 at 17:36
  • I have a custom list with checkbox. First user will select any element from the list by checking the checkbox and that element will get added in the ArrayList. But on deselecting the element by unchecking the checkbox, that element should get removed from the ArrayList. – Nikita Sep 17 '15 at 17:40
  • NameModel have an equals and hash code implemented ? – StackFlowed Sep 17 '15 at 17:42
  • No NameModel doesn't have equals and hashcode implemented. – Nikita Sep 17 '15 at 17:48

2 Answers2

2
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.

Marcelo
  • 4,580
  • 7
  • 29
  • 46
  • Yes Marcelo, you are correct, I just noticed that I'm creating a new model object everytime inside onClick event. Thanks. But can you elaborate more on what exactly do I need to implement in equals() and hashcode()? – Nikita Sep 17 '15 at 17:46
  • @user3098795 That depends on your business logic, but it seems from your code that you'd want to compare the `name` property. Check [this answer](http://stackoverflow.com/a/27609/1167210) for a great example of how to implement those methods. – Marcelo Sep 17 '15 at 17:51
  • Thanks Marcelo. I guess this will work, I'll try this way. – Nikita Sep 17 '15 at 18:34
0

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.

Simon Fischer
  • 1,154
  • 6
  • 22