arraylist.contains()方法返回false

时间:2019-03-01 10:02:42

标签: java android arraylist

我的活动中有一个全局数组列表,正在使用相同的列表添加用户并编辑用户信息。当我要从列表中删除对象时进行编辑时,即使列表包含该对象,ArrayList.contains()方法也会返回“ false”。因此,它不是从列表中删除对象。

我的活动代码

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");

                }
            }
        });
   }

}

在setEditData()方法中,我从User类对象获取性别列表为

  

genderList = user.getGender();

,然后当用户取消选中复选框时,应将其值从“ genderList”数组中删除。当我调试应用程序时,列表中存储了值,但是contains()方法仍然返回false,因此对象不会从列表中删除。

请告诉我这里是什么问题,或者我做错了什么?

0 个答案:

没有答案