0

I'm having a global arraylist in my activity.I'm using the same list to add the user and also to edit the user info. While editting when I want to remove an object from the list ,even if the list contains that object the ArrayList.contains() method returns "false".And so it is not removing the object from the list.

My activity code

public class MyActivity extends AppCompatActivity
        implements View.OnClickListener, Serializable {


    private List<Gender> genderList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_my);

        userList=new ArrayList();

        // bundle data
        Intent intent = getIntent ();
        if (intent != null) {

            User user= (User) intent.getSerializableExtra ("User");

            if (user!= null) {
                //set data
                setEditData (user);

            }

        }


        cbMale.setOnCheckedChangeListener (new CompoundButton.OnCheckedChangeListener () {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (genderList.contains (new Gender ("1"))) {

                    } else genderList.add (new Gender ("1"));
                } else {
                    if (genderList.contains (new Gender ("1"))) {
                        Log.e ("TAG", "List contains value");
                        genderList.remove(new Gender ("1");
                         }
                    else
                        Log.e ("TAG", "List does not contains value");

                }
            }
        });

        cbFemale.setOnCheckedChangeListener (new CompoundButton.OnCheckedChangeListener () {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (genderList.contains (new Gender ("2"))) {

                    } else genderList.add (new Gender ("2"));
                } else {
                    if (genderList.contains (new Gender ("2"))) {
                        Log.e ("TAG", "List contains value");
                        genderList.remove(new Gender ("2");
                         }
                    else
                        Log.e ("TAG", "List does not contains value");

                }
            }
        });
   }

}

In the setEditData() method ,I am getting the gender list from the User class object as

genderList = user.getGender();

and then when the user unchecks a checkbox ,it's value should be removed from the "genderList" array .When I debug the application, the list is having values stored in it but still the contains() method returns false and so the object is not removed from the list .

Please tell me what is the problem here or am I doing something wrong ?

Masum
  • 1,037
  • 15
  • 32
Neha
  • 389
  • 4
  • 8
  • 24

0 Answers0